답안 #350204

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
350204 2021-01-19T04:14:59 Z Kevin_Zhang_TW 낙하산 고리들 (IOI12_rings) C++17
37 / 100
2538 ms 189292 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L) == R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

const int MAX_N = 1000005;

int N, glob;

struct dsu {
	vector<int> g, sz, deg;

	int n, badcnt, allbad, answer;

	dsu() {};
	dsu (int n, int size) : n(n), answer(size), badcnt(0), allbad(0) { 
		g.resize(n);
		sz.resize(n, 1);
		deg.resize(n);
		iota(AI(g), 0); 
	}

	int F(int i) { return i == g[i] ? i : g[i] = F(g[i]); }

	void M(int a, int b) { 

		if (max(++deg[a], ++deg[b]) >= 3) allbad = true;

		if (allbad) return;

		a = F(a), b = F(b);
		if (sz[a] < sz[b]) swap(a, b);

		if (a != b) {
			return sz[a] += sz[b], g[b] = a, void();
		}
		if (++badcnt == 2) allbad = true;
		// merging into a ring
		answer = sz[a];

	}

	int getans() { 
		return allbad ? 0 : answer;
	}
};

// dsu adsu[20010];

dsu alld;

vector<int> edge[MAX_N];

bool iscan[MAX_N];

vector<int> big, can;

vector<dsu> forcan;

void canpush(int id) { if (iscan[id]) return; iscan[id] = true, can.pb(id); }

void Init(int N_) {

	assert(N <= 20000);

	N = N_;
	glob = N;
	alld = dsu(N, N);
// 	for (int i = 0;i < N;++i)
// 		adsu[i] = dsu(N, N-1);
}

vector<pair<int,int>> alledge;
// TODO big crisis
// new edge is {a, b}
void adjust(int a, int b) {
	DE(a, b);
	if (glob == -1) return;

	if (big.empty()) {
		alld.M(a, b);
		glob = alld.getans();
		alledge.pb(a, b);
		return;
	}

	glob = 0;

	int oldsize = forcan.size();
	// put new element into candidate
	for (int x : big) {
		canpush(x);
		for (int u : edge[x]) canpush(u);
	}
	// cerr << "a lot of candidate\n";
	// debug(AI(can));

	// examine new candidates

	for (int i = oldsize;i < can.size();++i) {
		forcan.pb(N, N-1);
		int x = can[i];
		for (auto [a, b] : alledge) if (a != x && b != x) {
			forcan[i].M(a, b);
		}
	}

	// examine all candidates
	for (int i = 0;i < can.size();++i) {
		int x = can[i];
		if (a != x && b != x)
			forcan[i].M(a, b);
		// DE(x, forcan[i].getans(), N-1);
		glob += forcan[i].getans() == N - 1;
	}

	alledge.pb(a, b);
}
void Link(int A, int B) {

// 
// 	for (int i = 0;i < N;++i) if (i != A && i != B)
// 		adsu[i].M(A, B);
// 
// 	return;

	if (glob == -1) return;
	edge[A].pb(B), edge[B].pb(A);
	if (edge[A].size() == 3) big.pb(A);
	if (edge[B].size() == 3) big.pb(B);
	if (big.size() > 3) {
		glob = -1;
		return;
	}
	adjust(A, B);
}

int CountCritical() {
// 	int res = 0;
// 	for (int i = 0;i < N;++i)
// 		if (adsu[i].getans() == N - 1) ++res;
// 	return res;
// 
	return max(0, glob);
}

Compilation message

rings.cpp: In constructor 'dsu::dsu(int, int)':
rings.cpp:25:25: warning: 'dsu::answer' will be initialized after [-Wreorder]
   25 |  int n, badcnt, allbad, answer;
      |                         ^~~~~~
rings.cpp:25:9: warning:   'int dsu::badcnt' [-Wreorder]
   25 |  int n, badcnt, allbad, answer;
      |         ^~~~~~
rings.cpp:28:2: warning:   when initialized here [-Wreorder]
   28 |  dsu (int n, int size) : n(n), answer(size), badcnt(0), allbad(0) {
      |  ^~~
rings.cpp: In function 'void adjust(int, int)':
rings.cpp:14:17: warning: statement has no effect [-Wunused-value]
   14 | #define DE(...) 0
      |                 ^
rings.cpp:89:2: note: in expansion of macro 'DE'
   89 |  DE(a, b);
      |  ^~
rings.cpp:112:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |  for (int i = oldsize;i < can.size();++i) {
      |                       ~~^~~~~~~~~~~~
rings.cpp:121:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |  for (int i = 0;i < can.size();++i) {
      |                 ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23788 KB Output is correct
2 Correct 19 ms 24300 KB Output is correct
3 Correct 23 ms 25324 KB Output is correct
4 Correct 18 ms 23916 KB Output is correct
5 Correct 18 ms 24044 KB Output is correct
6 Correct 19 ms 24172 KB Output is correct
7 Correct 19 ms 24556 KB Output is correct
8 Correct 18 ms 24044 KB Output is correct
9 Correct 20 ms 24428 KB Output is correct
10 Correct 20 ms 24556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 530 ms 50668 KB Output is correct
2 Correct 1821 ms 177604 KB Output is correct
3 Correct 401 ms 189292 KB Output is correct
4 Correct 1271 ms 74696 KB Output is correct
5 Correct 1276 ms 74960 KB Output is correct
6 Correct 1231 ms 73332 KB Output is correct
7 Correct 394 ms 177460 KB Output is correct
8 Correct 2131 ms 137380 KB Output is correct
9 Correct 2538 ms 156916 KB Output is correct
10 Correct 897 ms 72524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23788 KB Output is correct
2 Correct 19 ms 24300 KB Output is correct
3 Correct 23 ms 25324 KB Output is correct
4 Correct 18 ms 23916 KB Output is correct
5 Correct 18 ms 24044 KB Output is correct
6 Correct 19 ms 24172 KB Output is correct
7 Correct 19 ms 24556 KB Output is correct
8 Correct 18 ms 24044 KB Output is correct
9 Correct 20 ms 24428 KB Output is correct
10 Correct 20 ms 24556 KB Output is correct
11 Correct 20 ms 24428 KB Output is correct
12 Correct 24 ms 25196 KB Output is correct
13 Incorrect 29 ms 25324 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23788 KB Output is correct
2 Correct 19 ms 24300 KB Output is correct
3 Correct 23 ms 25324 KB Output is correct
4 Correct 18 ms 23916 KB Output is correct
5 Correct 18 ms 24044 KB Output is correct
6 Correct 19 ms 24172 KB Output is correct
7 Correct 19 ms 24556 KB Output is correct
8 Correct 18 ms 24044 KB Output is correct
9 Correct 20 ms 24428 KB Output is correct
10 Correct 20 ms 24556 KB Output is correct
11 Correct 20 ms 24428 KB Output is correct
12 Correct 24 ms 25196 KB Output is correct
13 Incorrect 29 ms 25324 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23788 KB Output is correct
2 Correct 19 ms 24300 KB Output is correct
3 Correct 23 ms 25324 KB Output is correct
4 Correct 18 ms 23916 KB Output is correct
5 Correct 18 ms 24044 KB Output is correct
6 Correct 19 ms 24172 KB Output is correct
7 Correct 19 ms 24556 KB Output is correct
8 Correct 18 ms 24044 KB Output is correct
9 Correct 20 ms 24428 KB Output is correct
10 Correct 20 ms 24556 KB Output is correct
11 Correct 530 ms 50668 KB Output is correct
12 Correct 1821 ms 177604 KB Output is correct
13 Correct 401 ms 189292 KB Output is correct
14 Correct 1271 ms 74696 KB Output is correct
15 Correct 1276 ms 74960 KB Output is correct
16 Correct 1231 ms 73332 KB Output is correct
17 Correct 394 ms 177460 KB Output is correct
18 Correct 2131 ms 137380 KB Output is correct
19 Correct 2538 ms 156916 KB Output is correct
20 Correct 897 ms 72524 KB Output is correct
21 Correct 20 ms 24428 KB Output is correct
22 Correct 24 ms 25196 KB Output is correct
23 Incorrect 29 ms 25324 KB Output isn't correct
24 Halted 0 ms 0 KB -