| Time Limit: 2000MS | Memory Limit: 10000K | |
| Total Submissions: 8409 | Accepted: 3931 |
Description
Your program should find the minimum number of soldiers that Bob has to put for a given tree.
For example for the tree:

Input
- the number of nodes
- the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifiernumber_of_roads
or
node_identifier:(0)
Output
Sample Input
4 0:(1) 1 1:(2) 2 3 2:(0) 3:(0) 5 3:(3) 1 4 2 1:(1) 0 2:(0) 0:(0) 4:(0)
Sample Output
1 2
鲍勃喜欢玩战略游戏。现在有n个城市,他们构成了一棵树。| Status | Accepted |
|---|---|
| Time | 234ms |
| Memory | 1804kB |
| Length | 1393 |
| Lang | G++ |
| Submitted | 2017-07-13 17:40:12 |
| Shared |
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int Max = 1500;
int num[Max + 5];
vector<int>Tr[Max + 5];
queue<int>Q;
bool vis[Max + 5];
bool getint(int & num){
char c; int flg = 1; num = 0;
while((c = getchar()) < '0' || c > '9') if(c == '-') flg = -1;
while(c >= '0' && c <= '9') { num = num * 10 + c - 48; c = getchar();}
num *= flg;
return 1;
}
int main(){
int N;
while(~scanf("%d", &N)){
int r, x, n;
for(int i = 1; i <= N; ++ i){
getint(r),getint(n);
for(int j =1; j <= n; ++ j)
getint(x),
Tr[r].push_back(x),
Tr[x].push_back(r),
++ num[x], ++ num[r];
}
if(N == 1){
printf("1\n");
continue;
}
for(int i = 0; i < N; ++ i)
if(num[i] == 1)
Q.push(i);
int Ans = 0;
while(! Q.empty()){
int tmp = Q.front(); Q.pop();
if(vis[tmp]) continue;
for(int i = 0; i < (int)Tr[tmp].size(); ++ i) if(! vis[Tr[tmp][i]]){
++ Ans;
int u = Tr[tmp][i];
vis[u] = 1;
for(int j = 0; j < (int)Tr[u].size(); ++ j){
-- num[Tr[u][j]];
if(! vis[Tr[u][j]] && num[Tr[u][j]] == 1)
Q.push(Tr[u][j]);
}
}
}
printf("%d\n", Ans);
memset(num, 0, sizeof(num));
memset(vis, 0, sizeof(vis));
for(int i = 0; i < N; ++ i)
Tr[i].clear();
}
return 0;
}
1963


&spm=1001.2101.3001.11974&articleId=75164137&d=1&t=3&u=ea6f2f5c01e942159043dfa8baf106ed)

被折叠的 条评论
为什么被折叠?



