이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
const int MXN = 5005;
int n;
vector<int> adj[MXN];
vector<int> sub[MXN];
int tak[MXN];
int sol(int node) {
int sb = 0;
for (int i=0; i<sub[node].size(); i++) {
int s = sub[node][i];
sb += sol(s);
}
return tak[node] = sb + 1;
}
int boss(int mo) {
int via[MXN];
bool vis[MXN];
for (int i=0; i<MXN; i++) {
via[i] = INT_MAX;
sub[i].clear();
}
via[mo] = 0;
memset(vis, 0, sizeof vis);
// cout << "FOR: " << mo << endl;
queue<pair<int, int> > proc; // node, via
proc.push(make_pair(mo, -1));
vis[mo] = true;
while (!proc.empty()) {
pair<int, int> pr = proc.front(); proc.pop();
int nd = pr.first, vi = pr.second;
// cout << " > " << nd << " via " << vi << endl;
via[nd] = vi;
for (int i=0; i<adj[nd].size(); i++) {
int other = adj[nd][i];
// cout << " ; " << other << endl;
if (!vis[other]) {
vis[other] = true;
proc.push(make_pair(other, nd));
// cout << " : " << other << endl;
}
}
}
for (int i=1; i<=n; i++) {
if (via[i] == INT_MAX) return INT_MAX;
if (via[i] == -1) continue;
sub[via[i]].push_back(i);
}
sol(mo);
int s = 0;
for (int i=1; i<=n; i++) s += tak[i];
return s;
}
int main() {
cin >> n;
for (int i=1; i<=n; i++) {
int ac;
cin >> ac;
for (int j=0; j<ac; j++) {
int k;
cin >> k;
adj[k].push_back(i);
}
}
int best = INT_MAX;
for (int i=1; i<=n; i++) {
best = min(best, boss(i));
}
cout << best << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
bosses.cpp: In function 'int sol(int)':
bosses.cpp:12:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<sub[node].size(); i++) {
~^~~~~~~~~~~~~~~~~
bosses.cpp: In function 'int boss(int)':
bosses.cpp:40:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<adj[nd].size(); i++) {
~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |