Happy Vertices

来源:互联网 发布:mysql execute using 编辑:程序博客网 时间:2024/06/05 08:40

中文题意:求一个图中那些    拥有儿子节点数   比他爸爸拥有儿子节点数    更多的节点的总数目,文中称为快乐节点。

You are given a graph with NN vertices and MM edges. Master parent is the vertex which has no parent but may have 0 or more children. In any connected component of the graph,vertex with the lowest value in that component serves as the master parent. 
A vertex is called happy if it has more children than its parent. Count the number of happy vertices in the given graph.The graph has no cycles or self loops.

Input Format:

First line consists of two space separated integers denoting NN and MM and the following MM lines consist of two space separated integers XXand YY denoting there is an edge between vertices XX and YY.

Output Format:

Print the number of happy vertices in the graph.

Constraints:
1N1000001≤N≤100000
0MN10≤M≤N−1 
1X,YN1≤X,Y≤N

SAMPLE INPUT
 
4 31 22 32 4
SAMPLE OUTPUT
 
1
Explanation

In this graph, we have vertices 1,2,3 and 4. Since 1 is the lowest among these, so it becomes the master vertex. Now, 1 has only 1 child while 2 has two children.So, 2 is a happy vertex. There are no more happy vertices in the graph.

Time Limit:1.0 sec(s) for each input file.
Memory Limit:256 MB
Source Limit:

1024 KB

#include <iostream>#include <vector>using namespace std;vector <int>adj[100005];bool visited[100005];int counti= 0 ;int children[100005];int T=0;void dfs(int s){visited[s] = true; for (int i = 0; i<adj[s].size(); ++i){if (visited[adj[s][i]] == false){if (T == 0){if (adj[s].size()<adj[adj[s][i]].size()-1)counti++;T = 1;}else{if (adj[s].size()<adj[adj[s][i]].size())counti++;}dfs(adj[s][i]);} }}void initialize(){ for (int i = 0; i < 100005; ++i)visited[i] = false;}int main() {ios_base::sync_with_stdio(false); cin.tie(0);int nodes, edges, count = 0, x, y;cin >> nodes >> edges;for (int i = 0; i < edges; ++i){cin >> x >> y;adj[x].push_back(y);adj[y].push_back(x); }initialize();for (int i = 1; i <= nodes; ++i){if (visited[i] == false)dfs(i);T = 0;}cout << counti<< endl;}