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>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
#define sz(x) ll(x.size())
#define base 1000000
#define M ll(1e9+7)
#define N 3005
#define F first
#define S second
#define pb push_back
#define in insert
#define eb emplace_back
#define ed "\n"
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef short int si;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int rk[N], pr[N], n, a[N][N], clr[N], kol, koler[N], h[N];
vector <int> g[N];
vector <pair <int, int> > vr;
int fnd(int x) {if (pr[x] != x) pr[x] = fnd(pr[x]); return pr[x];}
void link(int x, int y) {if (fnd(x) == fnd(y)) return; g[x].pb(y); g[y].pb(x); x = fnd(x); y = fnd(y); if (rk[x] < rk[y]) swap(x, y); rk[x] += rk[y]; pr[y] = x;}
void rec(int v, int p)
{
if (p != -1) h[v] = h[p] + 1;
for (auto it : g[v])
{
if (it == p) continue;
rec(it, v);
}
}
void dfs(int v, int kl, int from, int p)
{
if (v != from)
{
if (clr[v] == 0) return;
koler[clr[v]]++;
if (koler[clr[v]] == 1) kl++;
if (a[v][from] == kl && clr[from] == 0) clr[from] = clr[v];
}
for (auto it : g[v])
{
if (it == p) continue;
dfs(it, kl, from, v);
}
koler[clr[v]]--;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> n;
for (int i = 1; i <= n; i++) {rk[i] = 1; pr[i] = i;}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (a[i][j] == 1) link(i, j);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (a[i][j] == 2) link(i, j);
rec(1, -1);
for (int i = 1; i <= n; i++) vr.pb({h[i], i});
sort(vr.begin(), vr.end());
for (auto it : vr)
{
memset(koler, 0, sizeof(koler));
dfs(it.S, 0, it.S, -1);
if (clr[it.S] == 0) clr[it.S] = ++kol;
}
for (int i = 1; i <= n; i++)
cout << clr[i] << " ";
cout << '\n';
for (int i = 1; i <= n; i++)
{
sort(g[i].begin(), g[i].end());
for (auto it : g[i])
if (it > i) cout << i << " " << it << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |