#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 3007;
const ll inf = 2e9;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const double eps = 1e-6;
const int dx[] = {0, -1, 0, 1}, dy[] = {1, 0, -1, 0};
const int block = 320;
vector <int> adj[M];
vector < pair <int,int> > edges;
int n, p[M], sz[M], a[M][M], col[M], cur;
bool vist[M];
int root(int x){
while(x != p[x]){
p[x] = p[p[x]];
x = p[x];
}
return x;
}
void connect(int x, int y){
x = root(x); y = root(y);
if(x == y) return;
if(sz[x] > sz[y]) swap(x, y);
p[x] = y;
sz[y] += sz[x];
adj[x].pb(y); adj[y].pb(x);
edges.pb({x, y});
return;
}
bool check(int node, int p, int src1, int src2){
if(a[node][src1] == a[node][src2]){
col[src1] = col[node];
return 1;
}
for(auto i : adj[node]) if(i != p && vist[i]) if(check(i, node, src1, src2)) return 1;
return 0;
}
void dfs(int node, int p){
vist[node] = 1;
if(node == 1 || !check(p, node, node, p)) col[node] = ++cur;
for(auto i : adj[node]) if(i != p) dfs(i, node);
return;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> n;
for(int i = 1; i <= n; ++i) p[i] = i, sz[i] = 1;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
cin >> a[i][j];
for(int val = 1; val <= 2; ++val)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
if(a[i][j] == val) connect(i, j);
dfs(1, 0);
for(int i = 1; i <= n; ++i) cout << col[i] << " "; cout << endl;
for(auto i : edges) cout << i.F << " " << i.S << endl;
return 0;
}
Compilation message
izlet.cpp: In function 'int main()':
izlet.cpp:79:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
79 | for(int i = 1; i <= n; ++i) cout << col[i] << " "; cout << endl;
| ^~~
izlet.cpp:79:56: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
79 | for(int i = 1; i <= n; ++i) cout << col[i] << " "; cout << endl;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
485 ms |
35828 KB |
Output is correct |
3 |
Correct |
480 ms |
35832 KB |
Output is correct |
4 |
Correct |
501 ms |
35828 KB |
Output is correct |
5 |
Correct |
522 ms |
35768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
453 ms |
35868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
485 ms |
35828 KB |
Output is correct |
3 |
Correct |
480 ms |
35832 KB |
Output is correct |
4 |
Correct |
501 ms |
35828 KB |
Output is correct |
5 |
Correct |
522 ms |
35768 KB |
Output is correct |
6 |
Incorrect |
453 ms |
35868 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |