Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int preorder[1000000+5];
- vector<int> sasiedzi[1000000+5];
- int roz[1000000+5];
- bool odwiedzone[1000000+5];
- int temp_preorder = 1;
- int n;
- long long max_sciezka;
- long long suma_sciezek;
- int i=0;
- int make_Roz(int w)
- {
- i++;
- cout<<i<<endl;
- if(roz[w] != -1)
- return roz[w];
- else
- {
- odwiedzone[w] = true;
- int temp_roz = 1;
- int ile_sas = sasiedzi[w].size();
- for(int i=0; i<ile_sas; i++)
- {
- if(odwiedzone[sasiedzi[w][i]] == false)
- temp_roz += make_Roz(sasiedzi[w][i]);
- }
- roz[w] = temp_roz;
- i--;
- return temp_roz;
- }
- }
- void DFS(int w, long long dot_dlug)
- {
- odwiedzone[w] = true;
- dot_dlug++;
- suma_sciezek+=(2*dot_dlug);
- // cout<<"DODAJE 2 RAZY SCIEZKE DO: "<<w<<" rowna "<<dot_dlug<<endl;
- bool poszedlem_dalej = false;
- int ile_sas = sasiedzi[w].size();
- for(int i=0; i<ile_sas; i++)
- {
- if(odwiedzone[sasiedzi[w][i]] == false)
- {
- DFS(sasiedzi[w][i], dot_dlug);
- poszedlem_dalej=true;
- }
- }
- if(poszedlem_dalej == false)
- max_sciezka = max(max_sciezka, dot_dlug);
- }
- void DFS_bez_drugiego_C(int w, long long dot_dlug, int drugie_C)
- {
- odwiedzone[w] = true;
- if(w == drugie_C)
- return;
- dot_dlug++;
- //cout<<"DODAJE 2 RAZY SCIEZKE DO (B2C): "<<w<<" rowna "<<dot_dlug<<endl;
- bool poszedlem_dalej = false;
- int ile_sas = sasiedzi[w].size();
- for(int i=0; i<ile_sas; i++)
- {
- if(odwiedzone[sasiedzi[w][i]] == false)
- {
- DFS_bez_drugiego_C(sasiedzi[w][i], dot_dlug, drugie_C);
- poszedlem_dalej=true;
- }
- }
- if(poszedlem_dalej == false)
- max_sciezka = max(max_sciezka, dot_dlug);
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cin>>n;
- int t1;
- int t2;
- for(int i=1; i<n; i++)
- {
- cin>>t1;
- cin>>t2;
- sasiedzi[t1].push_back(t2);
- sasiedzi[t2].push_back(t1);
- }
- if(n==2)
- {
- cout<<1<<endl;
- cout<<1<<endl;
- return 0;
- }
- ///STWORZENIE TABLICY OJCOW I SYNOW
- /*cout<<"FATHER"<<endl;
- for(int i=1; i<=n; i++)
- cout<<i<<": "<<father[i]<<endl;*/
- for(int i=1; i<=n; i++)
- roz[i] = -1;
- make_Roz(1);
- // make_Preorder(1);
- for(int i=0; i<=n; i++)
- odwiedzone[i] = false;
- bool found_C = false;
- int ind = 1;
- while(found_C == false)
- {
- found_C = true;
- int ile_sas = sasiedzi[ind].size();
- for(int i=0; i<ile_sas; i++)
- {
- if(roz[sasiedzi[ind][i]] > n/2)
- {
- found_C = false;
- roz[ind] -= roz[sasiedzi[ind][i]];
- roz[sasiedzi[ind][i]] = n;
- ind = sasiedzi[ind][i];
- i=ile_sas+10;
- }
- }
- }
- int Centroid1 = ind;
- bool found_C2 = false;
- ///DLA KAZDEGO SASIADA CENTROIDU 1 SPRAWDZ CZY TEZ NIE JEST CENTROIDEM
- int ile_sas = sasiedzi[Centroid1].size();
- int Centroid2 = -1;
- ///POMIMO PETLI W PETLI W SUMIE ROZPATRZYMY KAZDY WIERZCHOLEK MAX 1 RAZ -> O(n)
- for(int i=0; i<ile_sas; i++)
- {
- int kandydat = sasiedzi[Centroid1][i];
- roz[Centroid1] -= roz[kandydat];
- roz[kandydat] = n;
- int ile_sas_kandydata = sasiedzi[kandydat].size();
- bool foundC2 = true;
- for(int j=0; j<ile_sas_kandydata; j++)
- {
- if(roz[sasiedzi[kandydat][j]] > n/2)
- {
- foundC2 = false;
- j=ile_sas+10;
- }
- }
- if(foundC2 == true)
- {Centroid2 = kandydat; break;}
- roz[kandydat] -= roz[Centroid1];
- roz[Centroid1] = n;
- }
- ///ZNALEZNIONO CENTROID(Y)
- //cout<<Centroid1<<" "<<Centroid2<<endl;
- max_sciezka = 0;
- suma_sciezek = 0;
- DFS(Centroid1, -1);
- long long dlu_C1 = suma_sciezek - max_sciezka;
- long long dlu_C2;
- //cout<<"CENT1: "<<Centroid1<<" dlu1:" <<dlu_C1<<" max_sciezka: "<<max_sciezka<<endl;
- if(Centroid2!=-1)
- {
- for(int i=0; i<=n; i++)
- odwiedzone[i] = false;
- max_sciezka = 0;
- DFS_bez_drugiego_C(Centroid2, 0, Centroid1);
- dlu_C1 = suma_sciezek - max_sciezka;
- // cout<<"TERAZ C2"<<endl;
- max_sciezka = 0;
- suma_sciezek = 0;
- for(int i=0; i<=n; i++)
- odwiedzone[i] = false;
- DFS(Centroid2, -1);
- for(int i=0; i<=n; i++)
- odwiedzone[i] = false;
- max_sciezka = 0;
- DFS_bez_drugiego_C(Centroid1, 0, Centroid2);
- dlu_C2 = suma_sciezek - max_sciezka;
- }
- // cout<<"CENT1: "<<Centroid1<<" dlu1:" <<dlu_C1<<" max_sciezka: "<<max_sciezka<<endl;
- // cout<<"CENT2: "<<Centroid2<<" dlu2:" <<dlu_C2<<" max_sciezka: "<<max_sciezka<<endl;
- for(int i=1; i<=n; i++)
- {
- if(i==Centroid1)
- cout<<dlu_C1<<'\n';
- else if(i==Centroid2)
- cout<<dlu_C2<<'\n';
- else
- cout<<-1<<'\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement