Link: http://www.spoj.com/problems/PT07X/
This question can be done with greedy ideas. node A is associated with Node B. If the level of node A is 1, it is better to delete node B, in this way, find all nodes with a degree of 1, delete the nodes associated with it, and know that there is no node with a degree of 1.
# Include <cstdio> # include <cstring> # include <iostream> # include <vector> # include <queue> # define maxn 100010 using namespace STD; vector <int> edge [maxn]; queue <int> q; int node [maxn], vis [maxn]; int main () {// freopen ("in.txt ", "r", stdin); int A, B, n, NN, ans; scanf ("% d", & N); nn = n-1; memset (node, 0, sizeof (node); memset (VIS, 0, sizeof (VIS); While (NN --) {scanf ("% d", & A, & B ); edge [A]. push_back (B); edge [B]. push_back (a); n Ode [a] ++; node [B] ++;} For (INT I = 1; I <= N; I ++) if (node [I] = 1) Q. push (I); ans = 0; while (! Q. empty () {int u = Q. front (); q. pop (); If (vis [u]) continue; For (INT I = 0; I <edge [u]. size (); I ++) {int v = edge [u] [I]; If (! Vis [v]) {vis [v] = 1; ans ++; For (Int J = 0; j <edge [v]. size (); j ++) {int T = edge [v] [J]; node [T] --; If (node [T] = 1 &&! Vis [T]) Q. Push (t) ;}}} cout <ans <Endl; return 0 ;}