답안 #760581

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
760581 2023-06-17T21:41:57 Z raysh07 슈퍼트리 잇기 (IOI20_supertrees) C++17
0 / 100
1 ms 468 KB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;

const int N = 1001;
int root[N];
vector <int> adj[N];
bool vis[N];
vector <int> comp;

int find(int x){
    if (root[x] == x) return x;
    return root[x] = find(root[x]);
}

void unite(int x, int y){
    x = find(x); y = find(y);
    
    if (x == y) return;
    root[x] = y;
}

void dfs(int u){
    vis[u] = true;
    comp.push_back(u);
    
    for (auto v : adj[u]){
        if (!vis[v]) dfs(v);
    }
}

int construct(vector<vector<int>> p) {
	int n = p.size();
	vector<vector<int>> ans(n, vector<int>(n, 0));
	
	for (int i = 0; i < n; i++) root[i] = i;
	
	for (int i = 0; i < n; i++){
	    for (int j = i + 1; j < n; j++){
	        if (p[i][j] == 1) unite(i, j);
	        if (p[i][j] == 3) return 0;
	    }
	}
	
	for (int i = 0; i < n; i++){
	    if (find(i) != i){
	        ans[i][find(i)] = ans[find(i)][i] = 1;
	    }
	}
	
	for (int i = 0; i < n; i++){
	    for (int j = i + 1; j < n; j++){
	        if (p[i][j] == 2 && find(i) == i && find(j) == j) {
	            adj[i].push_back(j);
	            adj[j].push_back(i);
	        }
	    }
	}
	
	for (int i = 0; i < n; i++){
	    if (vis[i] || find(i) != i) continue;
	    
	    comp.clear();
	    dfs(i);
	    
	    if (comp.size() == 2) return 0;
	    
	    for (int j = 0; j < comp.size() - 1; j++){
	        ans[comp[j]][comp[j + 1]] = ans[comp[j + 1]][comp[j]] = 1;
	    }
	}
	build(ans);
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:69:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |      for (int j = 0; j < comp.size() - 1; j++){
      |                      ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:35:46: warning: control reaches end of non-void function [-Wreturn-type]
   35 |  vector<vector<int>> ans(n, vector<int>(n, 0));
      |                                              ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 456 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 456 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 456 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 456 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -