#include <bits/stdc++.h>
#define DIM 100010
#define INF 2000000000
using namespace std;
vector <int> L[DIM],w[DIM],aux;
set <int> s;
int v[DIM],fth[DIM],sp[DIM],f[DIM],viz[DIM];
pair <int,int> d[DIM];
int n,i,x,y,sol_min,sol_max;
void dfs (int nod, int tata){
fth[nod] = tata;
sp[nod] = 1;
for (auto vecin : L[nod])
if (vecin != tata){
dfs (vecin,nod);
sp[nod] += sp[vecin];
}
if (nod != 1 && v[nod] == nod){
sol_min += 2;
swap (v[nod],v[fth[nod]]);
}
sol_max += 2 * min (sp[nod],n-sp[nod]);
}
int get_centroid (int nod, int tata){
int maxim = 0, heavy_son = 0;
for (auto vecin : L[nod]){
if (vecin == tata)
continue;
if (sp[vecin] > maxim)
maxim = sp[vecin], heavy_son = vecin;
}
if (maxim <= n/2 && n - sp[nod] <= n/2)
return nod;
else return get_centroid(heavy_son,nod);
}
void dfs2 (int nod, int idx){
s.insert(nod);
w[idx].push_back(nod);
viz[nod] = 1;
for (auto vecin : L[nod])
if (!viz[vecin])
dfs2 (vecin,idx);
}
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n;
for (i=1;i<n;i++){
cin>>x>>y;
L[x].push_back(y);
L[y].push_back(x);
}
for (i=1;i<=n;i++)
v[i] = i;
dfs (1,0);
for (i=1;i<=n;i++)
if (v[i] == i){
sol_min += 2;
if (i != 1)
swap (v[i],v[fth[i]]);
else swap (v[i],v[L[1][0]]);
}
cout<<sol_min<<" "<<sol_max<<"\n";
for (i=1;i<=n;i++)
cout<<v[i]<<" ";
cout<<"\n";
memset (v,0,sizeof v);
int centroid = get_centroid(1,0);
viz[centroid] = 1;
int mini = n, poz, k = 0;
for (auto vecin : L[centroid]){
dfs2 (vecin,vecin);
if (w[vecin].size() < mini)
mini = w[vecin].size(), poz = vecin;
d[++k] = make_pair(w[vecin].size(),vecin);
}
w[poz].push_back(centroid);
s.insert(centroid);
sort (d+1,d+k+1);
for (i=k;i;i--){
int vecin = d[i].second;
/// mai intai scot din set elemente care se afla in subarborele lui vecin
aux.clear();
for (auto it : w[vecin]){
if (s.find(it) != s.end())
aux.push_back(it); /// dupa trb sa le adaug la loc
s.erase(it);
}
for (auto it : w[vecin]){
v[it] = *s.begin();
s.erase(s.begin());
}
for (auto it : aux)
s.insert(it);
}
/*/// unde duc centroidul?
for (i=1;i<=n;i++)
f[v[i]]++;
for (i=1;i<=n;i++)
if (!f[i]){
v[centroid] = i;
break;
}
*/
for (i=1;i<=n;i++)
cout<<v[i]<<" ";
return 0;
}
Compilation message
Village.cpp: In function 'int main()':
Village.cpp:97:29: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
97 | if (w[vecin].size() < mini)
| ~~~~~~~~~~~~~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5324 KB |
Output is correct |
2 |
Correct |
4 ms |
5324 KB |
Output is correct |
3 |
Execution timed out |
1090 ms |
5324 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1092 ms |
5324 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5324 KB |
Output is correct |
2 |
Correct |
4 ms |
5324 KB |
Output is correct |
3 |
Execution timed out |
1090 ms |
5324 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |