이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define sz size()
#define x first
#define y second
#define pi pair <int, int>
#define pii pair <pi, int>
#define vi vector <int>
const ll mod = 1e9 + 7;
int n, q;
vector<int> g[10002];
int u[10001], d[10001];
int tst(int x){
queue <int> q;
for(int i=1; i<=n; i++)u[i]=0, d[i]=0;
q.push(x);
u[x]=1, d[x]=1;
while(!q.empty()){
int p=q.front();
q.pop();
for(int j=0; j<g[p].size(); j++){
int i=g[p][j];
if(u[i]==0){
u[i]=1;
d[i]=d[p]+1;
q.push(i);
}
}
}
int ans=0;
for(int i=1; i<=n; i++){
if(u[i]==0) return -1;
else ans+=d[i];
}
return ans;
}
int main(){
ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();
cin>>n;
for(int i=1; i<=n; i++){
int k;
cin>>k;
for(int j=1; j<=k; j++){
int x;
cin>>x;
g[x].pb(i);
}
}
int ans=1e9;
for(int i=1; i<=n; i++){
int cur=tst(i);
//cout<<i<<" "<<cur<<endl;
if(cur!=-1) ans=min(cur, ans);
}
if(ans!=1e9)cout<<ans<<endl;
else cout<<-1<<endl;
}
컴파일 시 표준 에러 (stderr) 메시지
bosses.cpp: In function 'int tst(int)':
bosses.cpp:26:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0; j<g[p].size(); j++){
~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |