Submission #97774

# Submission time Handle Problem Language Result Execution time Memory
97774 2019-02-18T12:45:10 Z E869120 Parachute rings (IOI12_rings) C++14
20 / 100
3362 ms 263168 KB
#include <bits/stdc++.h>
using namespace std;

class UnionFind {
	public:
	vector<int>G, H; vector<vector<int>> group; int CurrentSum, size_;
	vector<bool>I;
	
	void init(int sz){
		G.resize(sz, 0);
		H.resize(sz, 0);
		I.resize(sz, false);
		group.resize(sz);
		for (int i = 0; i < sz; i++) G[i] = i;
		for (int i = 0; i < sz; i++) group[i].push_back(i);
		CurrentSum = sz; size_ = sz;
	}
	void unite(int u, int v) {
		H[u]++; H[v]++;
		if (H[u] >= 3) { CurrentSum = 0; I[G[u]] = true; return; }
		if (H[v] >= 3) { CurrentSum = 0; I[G[v]] = true; return; }
		u = G[u]; v = G[v];
		if (group[u].size() < group[v].size()) swap(u, v);
		
		if (u == v) {
			if (I[u] == false) { I[u] = true; if (CurrentSum == size_) CurrentSum = (int)group[u].size(); else CurrentSum = 0; }
			return;
		}
		
		for (int i = 0; i < (int)group[v].size(); i++) {
			group[u].push_back(group[v][i]);
			G[group[v][i]] = u;
		}
		group[v].clear();
	}
};

int N, Q, cnts, M[1000009];
vector<pair<int, int>> G; vector<int> X[1000009]; 

// For rankers
int K[1000009]; UnionFind XX[1000009]; int KKK[1000009]; map<pair<int, int>,int> Map;
vector<pair<int, int>> rankers; vector<int>s3, s4;

int refs(int pos) {
	if (KKK[pos] >= 1) return KKK[pos];
	
	KKK[pos] = cnts + 1; cnts++; XX[cnts].init(N); K[cnts] = pos;
	for (int i = 0; i < (int)G.size(); i++) {
		int u = G[i].first, v = G[i].second; if (pos == u || pos == v) continue;
		XX[cnts].unite(u, v); //cout << pos << " " << cnts << " " << u << " " << v << " " << XX[cnts].CurrentSum << endl;
	}
	return cnts;
}

void refresh() {
	if ((int)s4.size() >= 2) { rankers.clear(); return; }
	if ((int)s3.size() >= 3) { rankers.clear(); return; }
	
	rankers.clear();
	for (int i = 0; i < (int)s3.size(); i++) rankers.push_back(make_pair(s3[i], refs(s3[i])));
	if (s4.size() == 0) {
		for (int t = 0; t < s3.size(); t++) {
			int pos1 = s3[t];
			for (int i = 0; i < (int)X[pos1].size(); i++) rankers.push_back(make_pair(X[pos1][i], refs(X[pos1][i])));
		}
	}
	sort(rankers.begin(), rankers.end());
	rankers.erase(unique(rankers.begin(), rankers.end()), rankers.end());
}

void Link_(int u, int v) {
	G.push_back(make_pair(u, v)); Map[make_pair(u, v)] = 1; Map[make_pair(v, u)] = 1;
	X[u].push_back(v);
	X[v].push_back(u);
	for (int i = 0; i < (int)rankers.size(); i++) {
		int pos = rankers[i].second;
		if (K[pos] == u || K[pos] == v) continue;
		XX[pos].unite(u, v); //cout << K[pos] << " " << pos << " " << u << " " << v << " " << XX[pos].CurrentSum << endl;
	}
	XX[0].unite(u, v);
	M[u]++; M[v]++;
	if (M[u] == 3) s3.push_back(u); if (M[u] == 4) s4.push_back(u);
	if (M[v] == 3) s3.push_back(v); if (M[v] == 4) s4.push_back(v);
	
	refresh();
	//cout << "#"; for (int i = 0; i < (int)rankers.size(); i++) cout << rankers[i].second << " "; cout << endl;
}

void Init(int N_) {
	N = N_; XX[0].init(N);
}

void Link(int A, int B) {
	Link_(A, B);
}

int CountCritical() {
	int ret = 0;
	if (cnts == 0) ret = XX[0].CurrentSum;
	else {
		for (int i = 0; i < (int)rankers.size(); i++) {
			int pos = rankers[i].second;
			if (XX[pos].CurrentSum == N) ret++;
		}
	}
	//cout << "ranker = " << rankers.size() << endl;
	return ret;
}

Compilation message

rings.cpp: In function 'void refresh()':
rings.cpp:63:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int t = 0; t < s3.size(); t++) {
                   ~~^~~~~~~~~~~
rings.cpp: In function 'void Link_(int, int)':
rings.cpp:83:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  if (M[u] == 3) s3.push_back(u); if (M[u] == 4) s4.push_back(u);
  ^~
rings.cpp:83:34: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  if (M[u] == 3) s3.push_back(u); if (M[u] == 4) s4.push_back(u);
                                  ^~
rings.cpp:84:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  if (M[v] == 3) s3.push_back(v); if (M[v] == 4) s4.push_back(v);
  ^~
rings.cpp:84:34: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  if (M[v] == 3) s3.push_back(v); if (M[v] == 4) s4.push_back(v);
                                  ^~
# Verdict Execution time Memory Grader output
1 Correct 145 ms 141484 KB Output is correct
2 Correct 163 ms 143456 KB Output is correct
3 Correct 157 ms 143892 KB Output is correct
4 Correct 138 ms 141624 KB Output is correct
5 Correct 136 ms 141996 KB Output is correct
6 Correct 143 ms 142712 KB Output is correct
7 Correct 148 ms 144248 KB Output is correct
8 Correct 143 ms 142392 KB Output is correct
9 Correct 157 ms 144888 KB Output is correct
10 Correct 146 ms 145200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 3362 ms 263168 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 145 ms 141484 KB Output is correct
2 Correct 163 ms 143456 KB Output is correct
3 Correct 157 ms 143892 KB Output is correct
4 Correct 138 ms 141624 KB Output is correct
5 Correct 136 ms 141996 KB Output is correct
6 Correct 143 ms 142712 KB Output is correct
7 Correct 148 ms 144248 KB Output is correct
8 Correct 143 ms 142392 KB Output is correct
9 Correct 157 ms 144888 KB Output is correct
10 Correct 146 ms 145200 KB Output is correct
11 Incorrect 156 ms 143836 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 145 ms 141484 KB Output is correct
2 Correct 163 ms 143456 KB Output is correct
3 Correct 157 ms 143892 KB Output is correct
4 Correct 138 ms 141624 KB Output is correct
5 Correct 136 ms 141996 KB Output is correct
6 Correct 143 ms 142712 KB Output is correct
7 Correct 148 ms 144248 KB Output is correct
8 Correct 143 ms 142392 KB Output is correct
9 Correct 157 ms 144888 KB Output is correct
10 Correct 146 ms 145200 KB Output is correct
11 Incorrect 156 ms 143836 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 145 ms 141484 KB Output is correct
2 Correct 163 ms 143456 KB Output is correct
3 Correct 157 ms 143892 KB Output is correct
4 Correct 138 ms 141624 KB Output is correct
5 Correct 136 ms 141996 KB Output is correct
6 Correct 143 ms 142712 KB Output is correct
7 Correct 148 ms 144248 KB Output is correct
8 Correct 143 ms 142392 KB Output is correct
9 Correct 157 ms 144888 KB Output is correct
10 Correct 146 ms 145200 KB Output is correct
11 Runtime error 3362 ms 263168 KB Execution killed with signal 9 (could be triggered by violating memory limits)
12 Halted 0 ms 0 KB -