이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 3;
vector<vector<int>> p, answer;
vector<int> adj[N], ans_adj[N], comp;
bool vis[N], c_vis[N];
int cnt[N], n;
void c_dfs(int u, vector<int> &line){
c_vis[u] = true;
line.push_back(u);
for(int to: comp)
if(!c_vis[to] && p[u][to] == 1 && u != to)
c_dfs(to, line);
}
void add_edge(int x, int y){
answer[x][y] = true;
answer[y][x] = true;
ans_adj[x].push_back(y);
ans_adj[y].push_back(x);
}
void solve_comp(){
//cout << "comp: "; for(int x: comp) cout << x << " "; cout << endl;
vector<vector<int>> lines;
for(int i: comp)
if(!c_vis[i]){
lines.push_back({});
c_dfs(i, lines.back());
}
for(auto &line: lines){
cout << "line: ";
for(int x: line)
cout << x << " ";
cout << endl;
}
for(auto &line: lines)
for(int i = 0; i < (int)line.size() - 1; ++i)
add_edge(line[i], line[i + 1]);
for(int i = 0; i < (int)lines.size(); ++i)
add_edge(lines[i][0], lines[(i + 1) % lines.size()][0]);
}
void dfs(int u){
vis[u] = true;
comp.push_back(u);
for(int i = 0; i < n; ++i)
if(i != u && p[i][u] && !vis[i])
dfs(i);
}
void valid_dfs(int u){
vis[u] = true;
cnt[u]++;
for(int to: ans_adj[u])
if(!vis[to])
valid_dfs(to);
vis[u] = false;
}
bool valid(){
for(int i = 0; i < n; ++i)
answer[i][i] = 0;
fill(vis, vis + n, 0);
for(int i = 0; i < n; ++i){
fill(cnt, cnt + n, 0);
valid_dfs(i);
for(int j = 0; j < n; ++j)
if(p[i][j] != cnt[j])
return false;
}
return true;
}
bool solve(){
for(int i = 0; i < n; ++i)
for(int j = i + 1; j < n; ++j){
if(p[i][j]){
adj[i].push_back(j);
adj[j].push_back(i);
}
}
for(int i = 0; i < n; ++i)
if(!vis[i]){
comp.clear();
dfs(i);
solve_comp();
}
return valid();
}
int construct(vector<vector<int>> _p) {
p = _p;
n = p.size();
answer.resize(n, vector<int>(n, 0));
if(solve()){
build(answer);
return 1;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |