记录编号 583478 评测结果 AAAAWTEEEE
题目名称 3644.[POJ 2762]从u到v还是从v到u? 最终得分 40
用户昵称 ┭┮﹏┭┮ 是否通过 未通过
代码语言 C++ 运行时间 1.751 s
提交时间 2023-10-16 16:37:23 内存使用 2.90 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];
int hd[N],tot,num,cnt,top;
int low[N],dfn[N],st[N],color[N];
int v[N],vr[N],vc[N];//vr为入度,vc为出度 
map<pair<int,int>,int>mp; 
void first(){
    tot = cnt = top = num = 0;
    memset(e,0,sizeof(e));
    memset(hd,0,sizeof(hd));
    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 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 check(int x){
    for(int i = hd[x];i;i = e[i].nx){
        int y = e[i].ver;
        if(color[x] == color[y])continue;
        if(mp[make_pair(x,y)])continue;//判重边? 
        vc[color[x]]++;
        vr[color[y]]++;
        mp[make_pair(x,y)]++;
    }
}
int main(){
    freopen("utov.in","r",stdin);
    freopen("utov.out","w",stdout);
    scanf("%d",&t);
    while(t--){
        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);
        for(int i = 1;i <= n;i++)
            check(i);
        int ans1 = 0,ans2 = 0;
        for(int i = 1;i <= num;i++){
            if(!vc[i])ans1++;
            if(!vr[i])ans2++;
        }
        if(max(ans1,ans2) <= 1)printf("Yes\n");
        else printf("No\n");
    }
    
    return 0;
    
}