이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
vector<int>parent;
vector<int>Size;
int findUpar(int node){
if(node == parent[node]) return node;
return parent[node] = findUpar(parent[node]);
}
void Union(int u, int v){
int pu = findUpar(u), pv = findUpar(v);
if(pu == pv) return;
if(Size[pu] > Size[pv]){
parent[pv] = pu; Size[pu] += Size[pv];
}else{
parent[pu] = pv; Size[pv] += Size[pu];
}
}
bool detectCycle(int src, vector<int> Adj[], vector<bool>&visited, int n){
visited[src] = true;
queue<pair<int,int>>q;
q.push({src, -1});
while(!q.empty()){
int node = q.front().first;
int par = q.front().second;
q.pop();
for(int adjNode: Adj[node]){
if(!visited[adjNode]){
visited[adjNode] = true;
q.push({adjNode, node});
}else if(par != adjNode){
return true;
}
}
}
return false;
}
int construct(vector<vector<int>>p){
vector<pair<int,int>>zero, two;
int n = p.size(); Size.resize(n); parent.resize(n);
for(int i = 0; i<n; i++) Size[i] = 1, parent[i] = i;
vector<vector<int>>adj(n, vector<int>(n, 0));
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(p[i][j] == 0){
zero.push_back({i, j});
}else if(p[i][j] == 1){
Union(i, j);
}else if(p[i][j] == 2){
two.push_back({i, j});
}else if(p[i][j] == 3){
return 0;
}
}
}
//mark 1
for(int i = 0; i<n; i++){
int pi = findUpar(i);
if(i != pi){
adj[i][pi] = 1; adj[pi][i] = 1;
}
}
//mark 2
vector<int>Adj[n];
for(pair<int,int> it: two){
int u = it.first, v = it.second;
int pu = findUpar(u), pv = findUpar(v);
if(pu == pv) return 0;
adj[pu][pv] = 1; adj[pv][pu] = 1;
Adj[pu].push_back(pv); Adj[pv].push_back(pu);
}
//check cycle
vector<bool>visited(n, false);
int cnt = 0;
for(int i = 0; i<n; i++){
if(!visited[i]){
bool cycle = detectCycle(i, Adj, visited, n);
if(cycle){
++cnt;
}
}
}
if(cnt) return 0;
//Union 2
for(pair<int,int> it: two){
Union(it.first, it.second);
}
//check 0
for(pair<int,int> it: zero){
int pu = findUpar(it.first), pv = findUpar(it.second);
if(pu == pv) return 0;
}
build(adj); return 1;
}
# | 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... |