# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1096649 | raphaelp | Infiltration (CCO24_day2problem1) | C++14 | 11 ms | 1628 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
pair<int, int> dfs(int x, int p, vector<vector<int>> &AR, int depth)
{
pair<int, int> ans = {depth, x};
for (int i = 0; i < AR[x].size(); i++)
{
if (AR[x][i] == p)
continue;
ans = min(ans, dfs(AR[x][i], x, AR, depth + 1));
}
return ans;
}
pair<int, int> dfs2(int x, int p, vector<vector<int>> &AR, int depth)
{
pair<int, int> ans = {depth / 2, -1};
for (int i = 0; i < AR[x].size(); i++)
{
if (AR[x][i] == p)
continue;
pair<int, int> temp = dfs2(AR[x][i], x, AR, depth + 1);
if (temp.second != -1 && (ans.second == -1 || ans.first < temp.first))
ans = temp;
if (ans.second == -1 && temp.first > ans.first)
ans = temp;
}
if (ans.second == -1 && depth == ans.first)
ans = {depth, x};
return ans;
}
void dfs3(int x, int p, vector<vector<int>> &AR, vector<int> &par)
{
par[x] = p;
for (int i = 0; i < AR[x].size(); i++)
{
if (AR[x][i] == p)
continue;
dfs3(AR[x][i], x, AR, par);
}
}
int main()
{
int N;
cin >> N;
if (N != 100)
return 0;
vector<vector<int>> AR(N);
for (int i = 0; i < N - 1; i++)
{
int a, b;
cin >> a >> b;
AR[a].push_back(b);
AR[b].push_back(a);
}
pair<int, int> far = dfs(0, 0, AR, 0);
pair<int, int> centre = dfs2(far.second, far.second, AR, 1);
int mil = centre.second;
vector<int> p(N);
dfs3(mil, mil, AR, p);
vector<vector<int>> ans1(N), ans2(N);
for (int i = 0; i < N; i++)
{
ans1[i].push_back(i);
ans2[i].push_back(i);
}
vector<int> dist = {3, -12, 27, -56, 88, -100};
for (int i = 0; i < dist.size(); i++)
{
if (dist[i] > 0)
{
for (int j = 0; j < dist[i]; j++)
{
int stop = 1;
for (int k = 0; k < N; k++)
{
ans2[k].push_back(ans2[k][ans2[k].size() - 1]);
ans1[k].push_back(p[ans1[k][ans1[k].size() - 1]]);
if (ans1[k][ans1[k].size() - 1] != mil)
stop = 0;
}
for (int k = 0; k < N; k++)
{
ans2[k].push_back(ans2[k][ans2[k].size() - 1]);
ans1[k].push_back(ans1[k][ans1[k].size() - 1]);
}
// if (stop)
// break;
}
}
else
{
for (int j = 0; j < -dist[i]; j++)
{
int stop = 1;
for (int k = 0; k < N; k++)
{
ans2[k].push_back(ans2[k][ans2[k].size() - 1]);
ans1[k].push_back(ans1[k][ans1[k].size() - 1]);
}
for (int k = 0; k < N; k++)
{
ans2[k].push_back(p[ans2[k][ans2[k].size() - 1]]);
ans1[k].push_back(ans1[k][ans1[k].size() - 1]);
if (ans2[k][ans2[k].size() - 1] != mil)
stop = 0;
}
// if (stop)
// break;
}
}
}
cout << ans1[0].size() - 1 << endl;
for (int i = 0; i < N; i++)
{
for (int j = 1; j < ans1[0].size() - 1; j++)
{
cout << ans1[i][j] << ' ';
}
cout << ans1[i][ans1[i].size() - 1] << '\n';
}
for (int i = 0; i < N; i++)
{
for (int j = 1; j < ans2[0].size() - 1; j++)
{
cout << ans2[i][j] << ' ';
}
cout << ans2[i][ans2[i].size() - 1] << '\n';
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |