Happy Vertices
来源:互联网 发布:mysql execute using 编辑:程序博客网 时间:2024/06/05 08:40
中文题意:求一个图中那些 拥有儿子节点数 比他爸爸拥有儿子节点数 更多的节点的总数目,文中称为快乐节点。
You are given a graph with N vertices and M 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 N and M and the following M lines consist of two space separated integers Xand Y denoting there is an edge between vertices X and Y.
Output Format:
Print the number of happy vertices in the graph.
Constraints:
1≤N≤100000
0≤M≤N−1
1≤X,Y≤N
4 31 22 32 4
1
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.
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;}
- Happy Vertices
- vertices
- happy!
- Happy
- Happy?
- happy
- happy!
- Happy
- happy
- happy
- Happy
- happy
- happy
- Happy
- happy
- happy
- happy!
- Happy!
- [Rcode]时间序列(整理自R in action)【待更新】
- Qt 隐藏鼠标指针
- Java解析excel操作
- PAT 1029. 旧键盘(20)
- 日历打印
- Happy Vertices
- [算法期中] 分组
- 8.11 应用举例
- 杭电ACM OJ 1031 Design T-Shirt 还是快速默写一波 快速排序 轻松搞定!
- Redis介绍,安装,连接。
- 半路出家的菜鸟程序员的历程
- 【软考学习】设计模式——适配器模式
- 在Ubuntu系统下安装和配置Matlab R2016b
- java中的输入输出流(字节流 字符流 节点流 过滤流)