WA26求debug
查看原帖
WA26求debug
1276544
luoguerepp楼主2024/3/29 22:01

入度为0的点有几个即是孤立城市的数量,为什么这样做行不通,是我遗漏了什么情况吗qwq 我看题解都是判断成环

WA26:expected 0,read 1 想不到为0是什么情况

#include<cstdio>
#include<cstring>

const int N = 1e5 + 10,M = 2 * N;

int n,m;
int h[N],e[M],ne[M],idx;
bool vis[N];
int in[N];

void dfs(int u,int f){
    for(int i = h[u]; i != -1; i = ne[i]){
        int j = e[i];
        if(vis[j] || j == f) continue;
        vis[j] = true;
        in[j]++;
        dfs(j,u);
    }
}
void add(int a,int b){
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}
int main(){
    memset(h,-1,sizeof h);
    
    scanf("%d%d",&n,&m);
    while(m--){
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b);
        add(b,a);
    }
    
    for(int i = 1; i <= n; ++i)
    	if(!vis[i]) dfs(i,-1);
    
    int ans = 0;
    for(int i = 1; i <= n; ++i) 
        if(!in[i])  ans++;
    printf("%d",ans);
    
    return 0;
}
2024/3/29 22:01
加载中...