11

Eric 阿蕾奇诺 2023-12-26 20:44:25 0

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

int n,m; int h[N],e[N],ne[N]; int idx; int ans = -1;

void add(int x, int y) { e[idx] = y; ne[idx] = h[x]; h[x] = idx++; }

int dfs(int g,int sum) { if(h[g] == -1) { return sum; } for(int i = h[g];i != -1;i = ne[i]) { int a = dfs(e[i],sum + 1); sum += a; } return sum; }

int main() { scanf("%d",&m); m --; int k = m; memset(h,-1,sizeof(h)); while (m--) { int y,x; scanf("%d%d",&x,&y); add(x,y); } if(h[1] == 0) { printf("1"); return 0; } for (int i = 0; i < k; i++) { printf("%d: ", i); for (int j = h[i]; j != -1; j = ne[j]) { printf("%d ", e[j]); } puts(""); } for (int i = h[1]; i != -1; i = ne[i]) { int a = dfs(e[i],1); ans = max(a,ans); } printf("%d",h[1] - ans); return 0; }

{{ vote && vote.total.up }}