Submission #782011

# Submission time Handle Problem Language Result Execution time Memory
782011 2023-07-13T14:45:11 Z bane Connecting Supertrees (IOI20_supertrees) C++17
21 / 100
188 ms 24140 KB
#include <bits/stdc++.h>
#include "supertrees.h"

using namespace std;

struct DisjointSetUnion{
	int N;
	vector<int>sz;
	vector<int>link;
	DisjointSetUnion(int n): N(n){
		sz.resize(n);
		link.resize(n);
		for (int i = 0; i < n; i++){
			link[i] = i;
			sz[i] = 1;
		}
	}

	int Find(int a){
		if (a == link[a])return a;
		return link[a] = Find(link[a]);
	}

	bool Unite(int a, int b){
		a = Find(a), b = Find(b);
		if (a == b)return false;
		if (sz[a] < sz[b])swap(a,b);
		sz[a] += sz[b];
		link[b] = a;
		return true;
	}
};


class Uniderected_Graph{
	public:
		int N, mx = 0, far = 0;
		vector<vector<int>>adj;
		vector<int>Visited;
		Uniderected_Graph(int _N) : N(_N){
			adj.resize(N);
			Visited.resize(N);
		}

		void Add_Edge(int A, int B){
			adj[A].push_back(B);
			adj[B].push_back(A);
		}

		void Far(int u, int p, int depth = 0){
			if (depth > mx){
				mx = depth;
				far = u;
			}
			Visited[u] = 1;
			for (int& nxt : adj[u]){
				if (nxt == p)continue;
				Far(nxt, u, depth + 1);
			}
		}

		pair<int,int> Find_Diameter(int Source){
			mx = -1, far = 0;
			Far(Source,Source,0);
			int A = far;
			mx = -1, far = 0;
			Far(A,A,0);
			int B = far;
			return make_pair(A,B);
		}
};



int construct(vector<vector<int>> p) {
	int n = (int)p.size();
	vector<vector<int>>adj(n,vector<int>(n));
	DisjointSetUnion DSU(n + 1);
	for (int i = 0; i < n; i++){
		for (int j = i + 1; j < n; j++){
			if (p[i][j] == 3)return 0;
			if (p[i][j] == 1){
				DSU.Unite(i,j);
			}
		}
	}

	//check if all p[i][j] = 1 in trees, otherwise contradiction
	for (int i = 0; i < n; i++){
		for (int j = i + 1; j < n; j++){
			int A = DSU.Find(i) , B = DSU.Find(j);
			if (p[i][j] != 1 && A == B){
				return 0;
			}
			if (A != i){
				adj[A][i] = adj[i][A] = 1;
			}
			if (B!=j){
				adj[B][j] = adj[j][B] = 1;
			}
		}
	}


	//All good

	//merge trees in cycle
	Uniderected_Graph G(n+1);

	for (int i = 0; i < n; i++){
		for (int j = i + 1; j < n; j++){
			int A = DSU.Find(i), B = DSU.Find(j);
			if (A == B)continue;
			//Different component
			if (p[A][B] == 2){
				DSU.Unite(A,B);
				G.Add_Edge(A,B);
				adj[A][B] = adj[B][A] = 1;
			}
		}
	}

	int Visited[n+1];
	for (int i = 0; i < n; i++){
		Visited[i] = 0;
	}

	for (int i = 0; i < n; i++){
		if (G.Visited[i])continue;
		pair<int,int>Connect = G.Find_Diameter(i);
		if (Connect.first == Connect.second)continue;
		if (adj[Connect.first][Connect.second])return 0; // cycle of length 2
		adj[Connect.first][Connect.second] = 1;
		adj[Connect.second][Connect.first] = 1;
	}

	//check if trees merged in cycle and have p[i][j] = 0, should be in another component
	//check if all needed are in same component
	for (int i = 0; i < n; i++){
		for (int j = i + 1; j < n; j++){
			int A = DSU.Find(i) , B = DSU.Find(j);
			if (p[i][j] == 0 && A == B)return 0;
			if (p[i][j] != 0 && A != B)return 0;
		}
	}

	build(adj);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:123:6: warning: variable 'Visited' set but not used [-Wunused-but-set-variable]
  123 |  int Visited[n+1];
      |      ^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 7 ms 1176 KB Output is correct
7 Correct 188 ms 23928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 7 ms 1176 KB Output is correct
7 Correct 188 ms 23928 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 6 ms 1208 KB Output is correct
13 Correct 146 ms 23844 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 82 ms 14084 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 38 ms 6216 KB Output is correct
21 Correct 157 ms 23928 KB Output is correct
22 Correct 183 ms 24140 KB Output is correct
23 Correct 161 ms 24012 KB Output is correct
24 Correct 147 ms 23928 KB Output is correct
25 Correct 61 ms 14064 KB Output is correct
26 Correct 65 ms 14068 KB Output is correct
27 Correct 160 ms 24036 KB Output is correct
28 Correct 150 ms 24052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 292 KB Output is correct
8 Correct 7 ms 1208 KB Output is correct
9 Correct 182 ms 23944 KB Output is correct
10 Incorrect 1 ms 212 KB Too few ways to get from 0 to 3, should be 2 found 1
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 39 ms 6192 KB Output is correct
5 Correct 153 ms 23976 KB Output is correct
6 Correct 157 ms 24040 KB Output is correct
7 Correct 166 ms 23932 KB Output is correct
8 Incorrect 1 ms 212 KB Too few ways to get from 0 to 3, should be 2 found 1
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 7 ms 1176 KB Output is correct
7 Correct 188 ms 23928 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 6 ms 1208 KB Output is correct
13 Correct 146 ms 23844 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 82 ms 14084 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 38 ms 6216 KB Output is correct
21 Correct 157 ms 23928 KB Output is correct
22 Correct 183 ms 24140 KB Output is correct
23 Correct 161 ms 24012 KB Output is correct
24 Correct 147 ms 23928 KB Output is correct
25 Correct 61 ms 14064 KB Output is correct
26 Correct 65 ms 14068 KB Output is correct
27 Correct 160 ms 24036 KB Output is correct
28 Correct 150 ms 24052 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 292 KB Output is correct
36 Correct 7 ms 1208 KB Output is correct
37 Correct 182 ms 23944 KB Output is correct
38 Incorrect 1 ms 212 KB Too few ways to get from 0 to 3, should be 2 found 1
39 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 7 ms 1176 KB Output is correct
7 Correct 188 ms 23928 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 6 ms 1208 KB Output is correct
13 Correct 146 ms 23844 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 296 KB Output is correct
16 Correct 3 ms 852 KB Output is correct
17 Correct 82 ms 14084 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 38 ms 6216 KB Output is correct
21 Correct 157 ms 23928 KB Output is correct
22 Correct 183 ms 24140 KB Output is correct
23 Correct 161 ms 24012 KB Output is correct
24 Correct 147 ms 23928 KB Output is correct
25 Correct 61 ms 14064 KB Output is correct
26 Correct 65 ms 14068 KB Output is correct
27 Correct 160 ms 24036 KB Output is correct
28 Correct 150 ms 24052 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 292 KB Output is correct
36 Correct 7 ms 1208 KB Output is correct
37 Correct 182 ms 23944 KB Output is correct
38 Incorrect 1 ms 212 KB Too few ways to get from 0 to 3, should be 2 found 1
39 Halted 0 ms 0 KB -