# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
312660 | luciocf | Izlet (COI19_izlet) | C++14 | 949 ms | 72684 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;
typedef pair<int, int> pii;
const int maxn = 3e3+10;
int a[maxn][maxn];
int cor[maxn];
vector<pii> edge;
vector<int> grafo[maxn];
struct DSU
{
int pai[maxn], peso[maxn];
void init(int n)
{
for (int i = 1; i <= n; i++)
pai[i] = i, peso[i] = 1;
}
int Find(int x)
{
if (pai[x] == x) return x;
return pai[x] = Find(pai[x]);
}
void Join(int x, int y)
{
x = Find(x), y = Find(y);
if (x == y) return;
if (peso[x] < peso[y]) swap(x, y);
pai[y] = x, peso[x] += peso[y];
}
} dsu;
int qtd[maxn], tot;
void dfs(int u, int p, int root)
{
if (!qtd[cor[u]]) ++tot;
++qtd[cor[u]];
if (a[root][u] < tot)
{
--qtd[cor[u]], --tot;
cor[u] = cor[root];
++qtd[cor[u]];
}
for (auto v: grafo[u])
if (v != p)
dfs(v, u, root);
--qtd[cor[u]];
if (qtd[cor[u]] == 0) --tot;
}
int main(void)
{
int s;
scanf("%d", &s);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
dsu.init(n);
int cc = 0;
for (int i = 1; i <= n; i++)
{
if (cor[i] == 0) cor[i] = ++cc;
for (int j = i+1; j <= n; j++)
{
if (a[i][j] == 1 && dsu.Find(i) != dsu.Find(j))
{
edge.push_back({i, j});
dsu.Join(i, j);
grafo[i].push_back(j);
grafo[j].push_back(i);
cor[j] = cor[i];
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = i+1; j <= n; j++)
{
if (a[i][j] == 2 && dsu.Find(i) != dsu.Find(j))
{
edge.push_back({i, j});
dsu.Join(i, j);
grafo[i].push_back(j);
grafo[j].push_back(i);
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= cc; j++)
assert(cor[j] == 0);
assert(tot == 0);
dfs(i, 0, i);
}
for (int i = 1; i <= n; i++)
printf("%d ", cor[i]);
printf("\n");
for (int i = 1; i < n; i++)
printf("%d %d\n", edge[i-1].first, edge[i-1].second);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |