1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include<bits/stdc++.h> using namespace std; #define Debug(x) cout<<#x<<':'<<x<<endl #define INF 0x7fffffff typedef long long ll; typedef pair<ll,ll> P; const ll maxn=1e2+11; const ll maxm=1e4+11; struct Edge{ ll to,nx; }e[maxm]; ll head[maxn],dr[maxn],vis[maxn],num[maxn]; ll tmp,n,m; set<ll> s; vector<P> edge; void init(){ s.clear();edge.clear(); memset(num,0,sizeof(num)); memset(head,0,sizeof(head)); memset(dr,0,sizeof(dr)); tmp=1; } void add(ll u,ll v){e[tmp]={v,head[u]};head[u]=tmp++;} void tuopu(){ for(ll i=1;i<=n;i++){ memset(vis,0,sizeof(vis)); queue<ll> q; ll cnt=0; q.push(i);vis[i]=1; while(!q.empty()){ ll u=q.front();q.pop(); cnt++; for(ll i=head[u];i;i=e[i].nx){ ll v=e[i].to; if(vis[v]) continue; vis[v]=1; q.push(v); } } if(cnt==n/2+1){ s.insert(i);num[i]++; } } } void solve(){ vector<ll> ans; tuopu(); memset(head,0,sizeof(head));memset(dr,0,sizeof(dr));tmp=1; for(auto i=edge.begin();i!=edge.end();i++){ ll u=(*i).second,v=(*i).first;add(u,v);dr[v]++; } tuopu(); for(auto i=s.begin();i!=s.end();i++){ if(num[*i]==2) ans.push_back(*i); } if(ans.size()==1) cout<<ans[0]<<endl; else cout<<0<<endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); ll ca;cin>>ca; while(ca--){ init(); cin>>n>>m; for(ll i=1;i<=m;i++){ ll u,v;cin>>u>>v;add(u,v);dr[v]++; edge.push_back({u,v}); } solve(); } return 0; }
|