-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.cpp
105 lines (94 loc) · 1.65 KB
/
Network.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Network.cpp
*
* Created on: Apr 25, 2015
* Author: viraj
*/
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#define INF 1000000000
using namespace std;
vector<vector<int> > neighbors;
vector<bool> visited;
int nodes;
int getvisitedtotal(){
int total=0;
for(int i=0;i<visited.size();++i){
if(visited[i]){
total++;
}
}
return total;
}
void dfs(int source,int ignore){
visited = vector<bool> (nodes,false);
stack<int> st;
st.push(source);
while(!st.empty()){
int node = st.top();
st.pop();
visited[node]=true;
for(int j=0;j<neighbors[node].size();++j){
if(neighbors[node][j]!=ignore&&!visited[neighbors[node][j]]){
st.push(neighbors[node][j]);
}
}
}
}
int main(){
while(true){
scanf("%d",&nodes);
if(nodes==0){
break;
}
neighbors = vector<vector<int> > (nodes,vector<int> ());
string temp;
while(getline(cin,temp)){
if(temp=="0"){
break;
}
//cout<<temp<<endl;
istringstream ss(temp);
int source;
ss >> source;
int neighbor;
while(ss >> neighbor){
neighbors[source-1].push_back(neighbor-1);
neighbors[neighbor-1].push_back(source-1);
}
}
int ans=0;
for(int i=0;i<nodes;++i){
if(i==0){
dfs(1,0);
}
else{
dfs(0,i);
}
int nnodes = getvisitedtotal();
if(nnodes<(nodes-1)){
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}