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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| #include <bits/stdc++.h> #define mp make_pair #define pb push_back #define pii pair<int,int> #define link(x) for(edge *j=h[x];j;j=j->next) #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,r,l) for(int i=r;i>=l;i--) const int MAXN=3e5+10; const int NM=1e6+10; const double eps=1e-8; const int mod=1e9+9; #define ll long long using namespace std; struct edge{int t,v;edge*next;}e[MAXN<<1],*h[MAXN],*o=e; void add(int x,int y,int vul){o->t=y;o->v=vul;o->next=h[x];h[x]=o++;} ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return x*f; }
int dfn[MAXN],low[MAXN],tot; bool kis[MAXN],Kis[MAXN]; stack<int>s; int Num; void tarjan_scc(int x){ dfn[x]=low[x]=++tot; s.push(x);kis[x]=1; link(x){ if(!dfn[j->t]){ tarjan_scc(j->t); low[x]=min(low[x],low[j->t]); } else if(kis[j->t]){ low[x]=min(low[x],dfn[j->t]); } } if(low[x]==dfn[x]){ Num++;Kis[x]=1; while(1){ int y=s.top(); if(y!=x)s.pop(),kis[y]=0,Kis[y]=1; else break; } } } typedef struct node{ int x,y; }node; node d[MAXN]; int fa[MAXN],dep[MAXN],num[MAXN],son[MAXN]; bool vis[MAXN],pis[NM]; vector<int>vec[MAXN]; int cnt,p[MAXN]; void dfs(int x,int pre,int deep){ fa[x]=pre;dep[x]=deep+1;num[x]=1;vis[x]=1; p[x]=++cnt; link(x){ if(vis[j->t])continue; pis[j->v]=1;vec[x].pb(j->t); dfs(j->t,x,deep+1); num[x]+=num[j->t]; if(son[x]==-1||num[son[x]]<num[j->t])son[x]=j->t; } } int tp[MAXN]; void _dfs(int x,int td){ tp[x]=td; if(son[x]!=-1)_dfs(son[x],td); for(int i=0;i<vec[x].size();i++)if(vec[x][i]!=fa[x]&&vec[x][i]!=son[x])_dfs(vec[x][i],vec[x][i]); } int Lca(int x,int y){ int xx=tp[x];int yy=tp[y]; while(xx!=yy){ if(dep[xx]<dep[yy])swap(xx,yy),swap(x,y); x=fa[xx];xx=tp[x]; } if(dep[x]>dep[y])swap(x,y); return x; } int sum[MAXN]; void __dfs(int x,int pre){ for(int i=0;i<vec[x].size();i++){ if(vec[x][i]==pre)continue; __dfs(vec[x][i],x); sum[x]+=sum[vec[x][i]]; } } int main(){ int _=read(); while(_--){ int n=read();cnt=0; memset(h,0,sizeof(h));o=e; memset(Kis,0,sizeof(Kis)); memset(dfn,0,sizeof(dfn)); inc(i,1,n)sum[i]=vis[i]=0,vec[i].clear(),son[i]=-1; Num=tot=0; int x,y;int cnt1=0; while(~scanf("%d%d",&x,&y)){ if(!x&&!y)break; ++cnt1;d[cnt1].x=x+1;d[cnt1].y=y+1; add(x+1,y+1,cnt1); } inc(i,1,n)if(!dfn[i])tarjan_scc(i); inc(i,1,cnt1)pis[i]=0; dfs(1,0,0); _dfs(1,1); bool flag=0; inc(i,1,cnt1){ if(pis[i])continue; int lca=Lca(d[i].x,d[i].y); if(lca==d[i].x){flag=1;break;} if(lca!=d[i].y){flag=1;break;} sum[d[i].x]++;sum[d[i].y]--; } inc(i,1,n)if(!Kis[i]){flag=1;break;} if(Num>=2){flag=1;break;} if(flag){printf("NO\n");continue;} __dfs(1,0); inc(i,2,n)if(sum[i]>1){printf("NO\n");flag=1;break;} if(!flag)printf("YES\n"); } return 0; }
|