Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <vector>
- using namespace std;
- vector<int> v[100005];
- vector <int> marked;
- int dist[100005];
- bool frecv[100005];
- int dfs (int nod, int og)
- {
- //cout<<nod<<endl;
- marked.push_back(nod);
- frecv[nod]=1;
- int maxi=0;
- for (int i=0; i<v[nod].size(); i++) {
- int next_nod=v[nod][i];
- //cout<<"."<<next_nod<<".";
- if (frecv[next_nod]==0) {
- //cout<<"-"<<next_nod<<"-";
- maxi=max(dist[og], dfs(next_nod, og)+1);
- }
- }
- //cout<<".";
- return maxi;
- }
- int main()
- {
- freopen("smallworld.in", "r", stdin);
- freopen("smallworld.out", "w",stdout);
- int n, a, b;
- cin>>n;
- for (int i=1; i<n; i++) {
- cin>>a>>b;
- v[a].push_back(b);
- v[b].push_back(a);
- }
- for (int i=1; i<=n; i++) {
- dist[i]=dfs(i, i);
- for (int i=0; i<marked.size(); i++) {
- frecv[marked[i]]=0;
- }
- marked.clear();
- cout<<dist[i]<<'\n';
- //cout<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement