记录编号 |
583575 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[POJ 2762]从u到v还是从v到u? |
最终得分 |
100 |
用户昵称 |
┭┮﹏┭┮ |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.000 s |
提交时间 |
2023-10-18 20:32:34 |
内存使用 |
0.00 MiB |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3+10,M = 6e3+10;
//tarjan求强连通分量 +缩点+拓扑序遍历
int t,n,m;
struct made{
int ver,nx;
}e[M],re[M];
int hd[N],rhd[N],tot,num,cnt,top;
int low[N],dfn[N],st[N],color[N];
int v[N];//vr为入度,vc为出度
map<pair<int,int>,int>mp;
void first(){
tot = cnt = top = num = 0;
memset(e,0,sizeof(e));
memset(re,0,sizeof(re));
memset(hd,0,sizeof(hd));
memset(rhd,0,sizeof(rhd));
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(st,0,sizeof(st));
memset(color,0,sizeof(color));
memset(v,0,sizeof(v));
}//初始化:(
void add(int x,int y){
tot++;
e[tot].ver = y,e[tot].nx = hd[x],hd[x] = tot;
}
void radd(int x,int y){
tot++;
re[tot].ver = y,re[tot].nx = rhd[x],rhd[x] = tot;
}
void tarjan(int x){
low[x] = dfn[x] = ++cnt;
st[++top] = x,v[x] = 1;
for(int i = hd[x];i;i = e[i].nx){
int y = e[i].ver;
if(!dfn[y])tarjan(y),low[x] = min(low[x],low[y]);
else if(v[y])low[x] = min(low[x],dfn[y]);
}
if(low[x] == dfn[x]){
num++;int y = 0;
do{
y = st[top--];
v[y] = 0,color[y] = num;
}while(x != y);
}
}
void push(int x){
for(int i = hd[x];i;i = e[i].nx){
int y = e[i].ver;
if(color[x] == color[y])continue;
v[color[y]]++;
radd(color[x],color[y]);
}
}
bool check(){//拓扑序遍历
queue<int>q;
for(int i = 1;i <= num;i++)
if(!v[i])q.push(i);
if(q.size() > 1)return 0;
while(!q.empty()){
int x = q.front();q.pop();
for(int i = rhd[x];i;i = re[i].nx){
int y = re[i].ver;
if(--v[y] == 0)q.push(y);
}
if(q.size() > 1)return 0;
}
return 1;
}
int main(){
freopen("utov.in","r",stdin);
freopen("utov.out","w",stdout);
scanf("%d",&t);
while(t--){
first();
scanf("%d%d",&n,&m);
for(int i = 1;i <= m;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
}
for(int i = 1;i <= n;i++)
if(!color[i])tarjan(i);
memset(v,0,sizeof(v));
tot = 0;
for(int i = 1;i <= n;i++)
push(i);
if(check())printf("Yes\n");
else printf("No\n");
}
return 0;
}