#include<bits/stdc++.h>
#define LL long long
#define fr(x) freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);
using namespace std;
const int N=3005;
int id,n,a[N][N],fa[N],c[N];bool vis[N][N],v[N];
basic_string<int>g[N];vector<pair<int,int>>ans;
inline void add(int u,int v){g[u]+=v,g[v]+=u;ans.push_back({u,v});}
inline int getf(int x){return fa[x]==x?x:fa[x]=getf(fa[x]);}
inline void hb(int x,int y){x=getf(x),y=getf(y);if(x^y) fa[y]=x,add(x,y);}
int m,col,rt;
void dfs1(int x,int fa,int s)
{
if(col==-1&&a[rt][x]==a[s][x]) col=c[x];
for(int i:g[x]) if(v[i]&&i!=fa) dfs1(i,x,s);
}
inline void get(int x)
{
rt=x;col=-1;
for(int i:g[x]) if(v[i]) dfs1(i,0,i);
c[x]=(col==-1?++m:col);
}
void dfs(int x)
{
v[x]=1;get(x);
for(int i:g[x]) if(!v[i]) dfs(i);
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>id>>n;
iota(fa+1,fa+1+n,1);
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++)
cin>>a[i][j],(a[i][j]==1)&&(hb(i,j),1);
basic_string<int>rt;
for(int i=1;i<=n;i++) if(fa[i]==i) rt+=i;
int m=rt.size();
for(int i=0;i<m;i++) for(int j=i+1;j<m;j++)
{
int u=rt[i],v=rt[j];
if(a[u][v]==2&&!vis[u][v])
{
basic_string<int>h{u,v};
for(int k=j+1;k<m;k++)
{
int w=rt[k];
if(a[u][w]==2&&a[v][w]==2) h+=w;
}
for(int k:h){for(int l:h) vis[k][l]=1;if(k!=h[0]) add(h[0],k);}
}
}
dfs(1);for(int i=1;i<=n;i++) cout<<c[i]<<" ";cout<<"\n";
for(auto [u,v]:ans) cout<<u<<" "<<v<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4668 KB |
Output is correct |
2 |
Correct |
439 ms |
53588 KB |
Output is correct |
3 |
Correct |
359 ms |
44672 KB |
Output is correct |
4 |
Correct |
404 ms |
58844 KB |
Output is correct |
5 |
Correct |
431 ms |
44416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
463 ms |
60660 KB |
Output is correct |
2 |
Correct |
401 ms |
56612 KB |
Output is correct |
3 |
Correct |
509 ms |
64140 KB |
Output is correct |
4 |
Correct |
486 ms |
64392 KB |
Output is correct |
5 |
Correct |
363 ms |
53332 KB |
Output is correct |
6 |
Correct |
396 ms |
40704 KB |
Output is correct |
7 |
Correct |
342 ms |
53588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4668 KB |
Output is correct |
2 |
Correct |
439 ms |
53588 KB |
Output is correct |
3 |
Correct |
359 ms |
44672 KB |
Output is correct |
4 |
Correct |
404 ms |
58844 KB |
Output is correct |
5 |
Correct |
431 ms |
44416 KB |
Output is correct |
6 |
Correct |
463 ms |
60660 KB |
Output is correct |
7 |
Correct |
401 ms |
56612 KB |
Output is correct |
8 |
Correct |
509 ms |
64140 KB |
Output is correct |
9 |
Correct |
486 ms |
64392 KB |
Output is correct |
10 |
Correct |
363 ms |
53332 KB |
Output is correct |
11 |
Correct |
396 ms |
40704 KB |
Output is correct |
12 |
Correct |
342 ms |
53588 KB |
Output is correct |
13 |
Correct |
437 ms |
58992 KB |
Output is correct |
14 |
Correct |
448 ms |
68908 KB |
Output is correct |
15 |
Correct |
386 ms |
58532 KB |
Output is correct |
16 |
Correct |
511 ms |
57012 KB |
Output is correct |
17 |
Correct |
468 ms |
55624 KB |
Output is correct |
18 |
Correct |
420 ms |
60676 KB |
Output is correct |
19 |
Correct |
424 ms |
60748 KB |
Output is correct |
20 |
Correct |
409 ms |
51208 KB |
Output is correct |
21 |
Correct |
402 ms |
57532 KB |
Output is correct |
22 |
Correct |
414 ms |
52868 KB |
Output is correct |
23 |
Correct |
421 ms |
60964 KB |
Output is correct |
24 |
Correct |
478 ms |
67216 KB |
Output is correct |
25 |
Correct |
462 ms |
56676 KB |
Output is correct |