#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
#include "supertrees.h"
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
struct DSU{
vector<int> par, Sz;
DSU(int n = 0){
par.resize(n + 3, 0);
Sz.resize(n + 3, 1);
iota(ALL(par), 0);
}
int root(int u){
return u == par[u] ? u : par[u] = root(par[u]);
}
bool join(int a, int b){
a = root(a);
b = root(b);
if(a == b) return false;
if(Sz[a] < Sz[b]) swap(a, b);
par[b] = a;
Sz[a] += Sz[b];
return true;
}
};
const int MAX = 1003;
int N;
int f[MAX][MAX], cnt[MAX];
bool visited[MAX];
vector<int> save;
void dfs1(int u){
if(visited[u]) return;
visited[u] = true;
save.push_back(u);
for(int v = 0; v < N; v++) if(f[u][v] == 1){
dfs1(v);
}
}
void dfs2(int u){
if(visited[u]) return;
visited[u] = true;
save.push_back(u);
for(int v = 0; v < N; v++) if(f[u][v] != 0){
dfs2(v);
}
}
int construct(vector<vector<int>> p){
N = sz(p);
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
f[i][j] = p[i][j];
if(p[i][j] == 3) return 0;
if(i != j && p[i][j] == 1){
cnt[i]++;
}
}
}
vector<vector<int>> ans(N, vector<int>(N, false));
DSU dsu(N);
for(int i = 0; i < N; i++) if(!visited[i]){
save.clear();
dfs1(i);
int sum = 0;
for(int x: save) sum += cnt[x];
if(sz(save) * (sz(save) - 1) != sum){
return 0;
}
for(int i = 1; i < sz(save); i++){
dsu.join(save[i], save[0]);
ans[save[i]][save[0]] = ans[save[0]][save[i]] = true;
}
for(int i = 0; i < sz(save); i++){
if(f[i][save[0]] == 2){
for(int x: save){
if(f[x][i] != 2) return 0;
}
}
}
}
memset(visited, false, sizeof visited);
for(int i = 0; i < N; i++) if(!visited[i]){
save.clear();
dfs2(i);
vector<int> v;
for(int x: save){
v.push_back(dsu.root(x));
}
cps(v);
if(sz(v) == 2){
return 0;
}
for(int j = 1; j < sz(v); j++){
ans[v[j - 1]][v[j]] = ans[v[j]][v[j - 1]] = true;
}
ans[v[0]][v.back()] = ans[v.back()][v[0]] = true;
}
build(ans);
return 1;
}
//int main(){
// ios_base::sync_with_stdio(0); cin.tie(0);
// // freopen("ADVERT.inp","r",stdin);
// // freopen("ADVERT.out","w",stdout);
//
// cout << construct({{1, 1, 2, 2}, {1, 1, 2, 2}, {2, 2, 1, 2}, {2, 2, 2, 1}});
//
// return 0;
//}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
b[0][0] is not 0 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
b[0][0] is not 0 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
b[0][0] is not 0 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
424 KB |
b[0][0] is not 0 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
b[0][0] is not 0 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
b[0][0] is not 0 |
2 |
Halted |
0 ms |
0 KB |
- |