PAT1021
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1021
思路:先用并查集求有几个连通分量,在用2次DFS 或者 BFS 即可,第一次随便选一个点,遍历一次, 找到最远结点之一,再冲最远结点之一便利一次 可得全部。
DFS:
1 #include<iostream> 2 #include<vector> 3 #include<set> 4 #include<algorithm> 5 #include<iterator> 6 using namespace std; 7 8 9 struct Node 10 { 11 int p; 12 int rank; 13 }; 14 15 void make_set(vector<Node> &ds, int x) 16 { 17 ds[x].p = x; 18 ds[x].rank = 0; 19 } 20 21 int find_set(vector<Node> &ds, int x) 22 { 23 if(ds[x].p != x) 24 ds[x].p = find_set(ds, ds[x].p); 25 return ds[x].p; 26 } 27 28 void link(vector<Node> &ds, int x, int y) 29 { 30 if(ds[x].rank > ds[y].rank) 31 ds[y].p = x; 32 else 33 { 34 ds[x].p = y; 35 if(ds[x].rank == ds[y].rank) 36 ++ds[y].rank; 37 } 38 } 39 40 void Union(vector<Node> &ds, int x, int y) 41 { 42 link(ds, find_set(ds, x), find_set(ds, y)); 43 } 44 45 void DFS_visit(vector<int>* adj, int start, vector<int> &used, vector<int> &dist) 46 { 47 used[start] = 1; 48 for(int i=0; i < adj[start].size(); ++i) 49 if(used[ adj[start][i] ] == 0) 50 { 51 dist[adj[start][i]] = dist[start] + 1; 52 DFS_visit(adj, adj[start][i], used, dist); 53 } 54 } 55 56 void DFS_version(vector<int>* adj, int start, vector<int> &dist) 57 { 58 vector<int> used(dist.size(), 0); 59 dist[start] = 0; 60 DFS_visit(adj, start, used, dist); 61 } 62 63 int main() 64 { 65 int N; 66 while(cin>>N) 67 { 68 vector<int>* adj = new vector<int>[N]; 69 vector<Node> ds(N); 70 for(int i=0; i<N; ++i) 71 { 72 make_set(ds, i); 73 } 74 for(int i=0; i<N-1; ++i) 75 { 76 int j, k; cin>>j>>k; 77 Union(ds, j-1, k-1); 78 adj[j-1].push_back(k-1); 79 adj[k-1].push_back(j-1); 80 } 81 int components(0); 82 for(int i=0; i<ds.size(); ++i) 83 if(ds[i].p == i) 84 ++components; 85 if(components > 1) 86 cout<<"Error: "<<components<<" components"<<endl; 87 else 88 { 89 vector<int> dist(N, -1); 90 DFS_version(adj, 0, dist); 91 int x = *max_element(dist.begin(), dist.end()); 92 set<int> s; 93 for(int i=0; i<dist.size(); ++i) 94 if(dist[i] == x) 95 s.insert(i); 96 DFS_version(adj, *s.begin(), dist); 97 x = *max_element(dist.begin(), dist.end()); 98 for(int i=0; i<dist.size(); ++i) 99 if(dist[i] == x) 100 s.insert(i); 101 set<int>::iterator iter = s.begin(); 102 while(iter != s.end()) 103 { 104 cout<<1+*iter<<endl; 105 ++iter; 106 } 107 } 108 } 109 return 0; 110 }
BFS:
1 #include<iostream> 2 #include<vector> 3 #include<set> 4 #include<queue> 5 #include<iterator> 6 using namespace std; 7 8 9 struct Node 10 { 11 int p; 12 int rank; 13 }; 14 15 void make_set(vector<Node> &ds, int x) 16 { 17 ds[x].p = x; 18 ds[x].rank = 0; 19 } 20 21 int find_set(vector<Node> &ds, int x) 22 { 23 if(ds[x].p != x) 24 ds[x].p = find_set(ds, ds[x].p); 25 return ds[x].p; 26 } 27 28 void link(vector<Node> &ds, int x, int y) 29 { 30 if(ds[x].rank > ds[y].rank) 31 ds[y].p = x; 32 else 33 { 34 ds[x].p = y; 35 if(ds[x].rank == ds[y].rank) 36 ++ds[y].rank; 37 } 38 } 39 40 void Union(vector<Node> &ds, int x, int y) 41 { 42 link(ds, find_set(ds, x), find_set(ds, y)); 43 } 44 45 int BFS(vector<int>* adj, int start, vector<int> &dist) 46 { 47 vector<int> used(dist.size(), 0); 48 queue<int> q; 49 dist[start] = 0; 50 used[start] = 1; 51 q.push(start); 52 int heightest(0); 53 while(!q.empty()) 54 { 55 for(int i=0; i<adj[q.front()].size(); ++i) 56 if(used[adj[q.front()][i]] == 0) 57 { 58 dist[adj[q.front()][i]] = dist[q.front()]+1; 59 heightest = dist[adj[q.front()][i]] > heightest ? dist[adj[q.front()][i]] : heightest; 60 q.push(adj[q.front()][i]); 61 used[adj[q.front()][i]] = 1; 62 } 63 q.pop(); 64 } 65 return heightest; 66 } 67 68 int main() 69 { 70 int N; 71 while(cin>>N) 72 { 73 vector<int>* adj = new vector<int>[N]; 74 vector<Node> ds(N); 75 for(int i=0; i<N; ++i) 76 { 77 make_set(ds, i); 78 } 79 for(int i=0; i<N-1; ++i) 80 { 81 int j, k; cin>>j>>k; 82 Union(ds, j-1, k-1); 83 adj[j-1].push_back(k-1); 84 adj[k-1].push_back(j-1); 85 } 86 int components(0); 87 for(int i=0; i<ds.size(); ++i) 88 if(ds[i].p == i) 89 ++components; 90 if(components > 1) 91 cout<<"Error: "<<components<<" components"<<endl; 92 else 93 { 94 vector<int> dist(N, -1); 95 int x = BFS(adj, 0, dist); 96 set<int> s; 97 for(int i=0; i<dist.size(); ++i) 98 if(dist[i] == x) 99 s.insert(i); 100 101 x = BFS(adj, *s.begin(), dist); 102 for(int i=0; i<dist.size(); ++i) 103 if(dist[i] == x) 104 s.insert(i); 105 set<int>::iterator iter = s.begin(); 106 while(iter != s.end()) 107 { 108 cout<<1+*iter<<endl; 109 ++iter; 110 } 111 } 112 } 113 return 0; 114 }