답안 #432455

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
432455 2021-06-18T10:15:02 Z dreezy 슈퍼트리 잇기 (IOI20_supertrees) C++17
21 / 100
324 ms 24084 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e3  + 5;
#define pb push_back
struct UnionFind{
	int parent[maxn], height[maxn];
	
	void init(int n){
		for(int i =0; i<n; i++) parent[i] =i;
		memset(height, 0, sizeof height);
	}
	
	int root(int n){
		if(parent[n]!= n)
			parent[n] = root(parent[n]);
		return parent[n];
	}
	
	bool same(int n1, int n2){
		return root(n1) == root(n2);
	}
	
	void join(int n1, int n2){
		int r1 = root(n1);
		int r2 = root(n2);
		
		if(height[r1]> height[r2]){
			parent[r2] = r1;
			height[r1] = max(height[r1]+1, height[r2]);
		}
		else{
			parent[r1] = r2;
			height[r2] = max(height[r2]+1, height[r1]);
			
		}
	}
};


int construct(vector<vector<int>> p) {
	int n = p.size();
	
	UnionFind uf;
	uf.init(n);
	
	for(int i =0; i<n-1 ; i++){
		for(int j = i + 1; j<n; j++){
			//cout<<i<<", "<<j<<endl;
			if(p[i][j] > 0) uf.join(i, j);
		}
	}
	map<int, set<int>> trees;
	for(int i =0; i<n -1; i++){
		for(int j =i + 1; j< n; j++){
			if(p[i][j] == 0)
				if(uf.same(i, j))return 0;
			trees[uf.root(i)].insert(i);
			trees[uf.root(j)].insert(j);
		}
	}
	
	
	
	vector<vector<int>> ans(n, vector<int>(n, 0));
	
	
	for(auto e : trees){
		set<int> tree = e.second;
		//cout << e.first<<endl;
		vector<vector<int>> graph(tree.size(), vector<int>());
		UnionFind uft;
		uft.init(n);
		vector<int> degree(n, 0);
		int endpoint = -1;
		
		vector<bool> vis(n, 0);
		for(int i : tree){
			for(int j : tree){
				if(i == j) continue;
				//cout << i <<", "<<j<<endl;
				if(p[i][j] ==1){
					if(!uft.same(i, j)){
						vis[i] = vis[j] = true;
						degree[i]++;
						degree[j]++;
						ans[i][j] = ans[j][i] =1;
						uft.join(i, j);
					}
				}
			}
		}
		
		for(int i : tree)
			if(degree[i] == 1){
				endpoint = i;
				break;
			}
	
		int first = -1, last = -1;
		for(int i : tree){
			if(vis[i]) continue;
			for(int j : tree){
				if(vis[j]) continue;
				if(i == j) continue;
				if(p[i][j] >1){
					if(!uft.same(i, j)){
						if(first == -1) first = i;
						last = j;
						ans[i][j] = ans[j][i] = 1;
						uft.join(i, j);
						break;
					}
				}
			}
		}
		
		if(first == -1) continue;
		
		if(endpoint != -1){
			ans[endpoint][first] = ans[first][endpoint] = 1;
			ans[endpoint][last] = ans[last][endpoint] =1;
		}
		else{
			ans[last][first] = ans[first][last] = 1;
		}
		//cout <<endpoint<<endl;
		//cout << first<<", "<<last<<endl;
		
	}
	
	
	build(ans);
	return 1;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1172 KB Output is correct
7 Correct 301 ms 22028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1172 KB Output is correct
7 Correct 301 ms 22028 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1224 KB Output is correct
13 Correct 264 ms 22092 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 110 ms 8184 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 87 ms 5760 KB Output is correct
21 Correct 278 ms 22056 KB Output is correct
22 Correct 304 ms 22088 KB Output is correct
23 Correct 287 ms 22084 KB Output is correct
24 Correct 271 ms 22164 KB Output is correct
25 Correct 99 ms 8184 KB Output is correct
26 Correct 96 ms 8184 KB Output is correct
27 Correct 310 ms 22136 KB Output is correct
28 Correct 261 ms 22212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Incorrect 1 ms 204 KB Answer gives possible 1 while actual possible 0
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 73 ms 6212 KB Output is correct
5 Correct 281 ms 24016 KB Output is correct
6 Correct 281 ms 24076 KB Output is correct
7 Correct 324 ms 24056 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 72 ms 6244 KB Output is correct
10 Correct 280 ms 24084 KB Output is correct
11 Correct 283 ms 23984 KB Output is correct
12 Correct 324 ms 24056 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Incorrect 70 ms 6256 KB Too few ways to get from 0 to 24, should be 2 found 0
17 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1172 KB Output is correct
7 Correct 301 ms 22028 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1224 KB Output is correct
13 Correct 264 ms 22092 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 110 ms 8184 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 87 ms 5760 KB Output is correct
21 Correct 278 ms 22056 KB Output is correct
22 Correct 304 ms 22088 KB Output is correct
23 Correct 287 ms 22084 KB Output is correct
24 Correct 271 ms 22164 KB Output is correct
25 Correct 99 ms 8184 KB Output is correct
26 Correct 96 ms 8184 KB Output is correct
27 Correct 310 ms 22136 KB Output is correct
28 Correct 261 ms 22212 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Incorrect 1 ms 204 KB Answer gives possible 1 while actual possible 0
33 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1172 KB Output is correct
7 Correct 301 ms 22028 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1224 KB Output is correct
13 Correct 264 ms 22092 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 110 ms 8184 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 87 ms 5760 KB Output is correct
21 Correct 278 ms 22056 KB Output is correct
22 Correct 304 ms 22088 KB Output is correct
23 Correct 287 ms 22084 KB Output is correct
24 Correct 271 ms 22164 KB Output is correct
25 Correct 99 ms 8184 KB Output is correct
26 Correct 96 ms 8184 KB Output is correct
27 Correct 310 ms 22136 KB Output is correct
28 Correct 261 ms 22212 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Incorrect 1 ms 204 KB Answer gives possible 1 while actual possible 0
33 Halted 0 ms 0 KB -