이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define quick ios::sync_with_stdio(0);cin.tie(NULL); cout.tie(NULL);
typedef long long ll;
using namespace std;
const int N = 5001;
bool vis[N] = {false};
// bool prime[N+1];
vector<int>adj[N];
vector<int>v;
map<int,int>mp;
void bfs(int s) {
v.clear();
memset(vis,false,sizeof(vis));
queue<int>q;
vis[s] = true;
q.push(s);
v.push_back(s);
while (!q.empty()) {
int node = q.front();
q.pop();
for (int child : adj[node]) {
if (!vis[child]) {
vis[child] = true;
q.push(child);
v.push_back(child);
}
}
}
}
int main() {
quick;
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int k;
cin >> k;
for (int j = 0; j < k; j++) {
int u;
cin >> u;
adj[u].push_back(i);
// adj[i].push_back(u);
}
}
ll mn = LLONG_MAX;
ll sum;
for (int i = 1; i <= n; i++) {
int c = 0;
sum = 0;
bfs(i);
mp.clear();
if (v.size() != n)
continue;
reverse(all(v));
memset(vis,false,sizeof(vis));
for (int i = 0; i < v.size(); i++) {
vis[v[i]] = true;
for (int child : adj[v[i]]) {
if (vis[child]) {
mp[v[i]] += mp[child];
}
}
mp[v[i]]++;
sum += mp[v[i]];
}
mn = min(sum,mn);
}
cout << mn << endl;
// while (!root.empty()) {
// queue<int>q;
// int level[N];
// memset(level,0,sizeof(level));
//
//
// int u = root.front();
// q.push(u);
// level[u] = 1;
// vis[u] = true;
//
// while (!q.empty()) {
// int node = q.front();
// q.pop();
// for (int child : adj[node]) {
// if (!vis[child]) {
// vis[child] = true;
// q.push(child);
// level[child] = level[node] + 1;
// mx = max(mx, level[child]);
// }
// }
// }
// root.pop();
// }
// cout << mx << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
bosses.cpp: In function 'int main()':
bosses.cpp:60:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
60 | if (v.size() != n)
| ~~~~~~~~~^~~~
bosses.cpp:64:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
bosses.cpp:56:11: warning: unused variable 'c' [-Wunused-variable]
56 | int c = 0;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |