이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |