#include <iostream>
#include <vector>
#include <set>
#define MAXN 3005
int n;
int par1[MAXN];
int par2[MAXN];
int sec[MAXN][MAXN];
int f[MAXN][MAXN];
std::vector<int> Adj[MAXN];
std::vector<std::pair<int, int> > extra;
int root(int u, int par[])
{
if(par[u] < 0)
return u;
return par[u] = root(par[u], par);
}
void merge(int u, int v, int par[])
{
u = root(u, par);
v = root(v, par);
if(u == v)
return;
if(par[u] > par[v])
u ^= v ^= u ^= v;
par[u] += par[v];
par[v] = u;
}
void dfs(int u, int p, int x, int y)
{
sec[x][u] = y;
for(int v : Adj[u])
{
if(v != p)
dfs(v, u, x, y);
}
}
void check(int u, int p, int x, std::set<int> cur)
{
cur.insert(root(u, par1));
if(cur.size() != f[u][x])
std::cout << u << " " << x << " " << cur.size() << " " << f[u][x] << std::endl;
for(int v : Adj[u])
if(v != p)
check(v, u, x, cur);
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin >> n >> n;
for(int i = 0; i < n; i++)
par1[i] = par2[i] = -1;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
std::cin >> f[i][j];
if(f[i][j] == 1)
merge(i, j, par1);
}
}
for(int i = 0; i < n; i++)
{
if(par1[i] >= 0)
{
int j = root(i, par1);
Adj[i].push_back(j);
Adj[j].push_back(i);
continue;
}
for(int j = i+1; j < n; j++)
{
if(par1[j] >= 0)
continue;
if(f[i][j] == 2)
{
if(root(i, par2) == root(j, par2))
continue;
Adj[i].push_back(j);
Adj[j].push_back(i);
merge(i, j, par2);
}
}
}
for(int i = 0; i < n; i++)
{
if(par1[i] >= 0)
continue;
for(int j : Adj[i])
dfs(j, i, i, j);
}
for(int i = 0; i < n; i++)
{
if(par1[i] >= 0)
continue;
for(int j = i+1; j < n; j++)
{
if(par1[j] >= 0)
continue;
int x = sec[i][j];
int y = sec[j][i];
if((f[i][j] == f[x][j]) && (f[i][j] == f[i][y]) && (f[i][j] == (f[x][y] + 1)))
merge(i, j, par1);
}
}
// std::set<int> cur;
// for(int i = 0; i < n; i++)
// check(i, -1, i, cur);
// std::cout << " done " << std::endl;
for(int i = 0; i < n; i++)
std::cout << root(i, par1)+1 << " ";
std::cout << std::endl;
for(int i = 0; i < n; i++)
{
for(int j : Adj[i])
{
if(i < j)
std::cout << i+1 << " " << j+1 << std::endl;
}
}
// for(std::pair<int, int> x : extra)
// {
// std::cout << x.first+1 << " " << x.second+1 << std::endl;
// }
}
Compilation message
izlet.cpp: In function 'void check(int, int, int, std::set<int>)':
izlet.cpp:53:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(cur.size() != f[u][x])
~~~~~~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
806 ms |
71252 KB |
Output is correct |
3 |
Correct |
792 ms |
71168 KB |
Output is correct |
4 |
Correct |
730 ms |
51936 KB |
Output is correct |
5 |
Correct |
772 ms |
63772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
857 ms |
65792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
632 KB |
Output is correct |
2 |
Correct |
806 ms |
71252 KB |
Output is correct |
3 |
Correct |
792 ms |
71168 KB |
Output is correct |
4 |
Correct |
730 ms |
51936 KB |
Output is correct |
5 |
Correct |
772 ms |
63772 KB |
Output is correct |
6 |
Incorrect |
857 ms |
65792 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |