Submission #765769

# Submission time Handle Problem Language Result Execution time Memory
765769 2023-06-25T04:57:00 Z marvinthang Simurgh (IOI17_simurgh) C++17
51 / 100
87 ms 2208 KB
/*************************************
*    author: marvinthang             *
*    created: 25.06.2023 11:26:35    *
*************************************/

#include "simurgh.h"
#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i--; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }

struct DisjointSet {
    
    int n, *lab = nullptr;

    DisjointSet(int _n = 0) {
        resize(_n);
    }

    void reset(void) {
        memset(lab, -1, (n + 1) * sizeof(int));
    }

    void resize(int _n) {
         if (lab != nullptr) delete[] lab;
         n = _n;
         lab = new int[n + 1];
         reset();
    }

    int find(int u) {
        assert(u <= n);
        return lab[u] < 0 ? u : lab[u] = find(lab[u]);
    }

    bool connected(int u, int v) { return find(u) == find(v); }
    bool isRoot(int u) { return lab[u] < 0; }
    int size(int u) { return -lab[find(u)]; }

    bool join(int u, int v) {
        if ((u = find(u)) == (v = find(v))) return false;
        if (lab[u] > lab[v]) swap(u, v);
        lab[u] += lab[v];
        lab[v] = u;
        return true;
    }

};

using DSU = DisjointSet;

// end of template

vector <int> find_roads(int n, vector <int> u, vector <int> v) {
	int m = u.size();
	DSU dsu(n);
	vector <int> on_tree;
	vector <int> rem;
	REP(i, m) (dsu.join(u[i], v[i]) ? on_tree : rem).push_back(i);
	int cur = count_common_roads(on_tree);
	vector <int> res;
	// cout << on_tree << '\n';
	REP(i, n - 1) {
		dsu.reset();
		REP(j, n - 1) if (j != i) dsu.join(u[on_tree[j]], v[on_tree[j]]);
		int f = on_tree[i];
		vector <int> g;
		int in_i = 0;
		REPD(j, rem.size()) if (!dsu.connected(u[rem[j]], v[rem[j]])) {
			// cout << rem[j] << ' ' << u[rem[j]] << ' ' << v[rem[j]] << '\n';
			on_tree[i] = rem[j];
			int nxt = count_common_roads(on_tree);
			if (nxt > cur) {
				in_i = -1;
				res.push_back(rem[j]);
			} else if (nxt < cur) {
				if (!in_i) {
					res.push_back(f);
					in_i = 1;
				}
			} else g.push_back(rem[j]);
			swap(rem[j], rem.back());
			rem.pop_back();
		}
		// cout << in_i << '\n';
		if (in_i == 1) res.insert(res.end(), ALL(g));
		else if (!in_i) {
			bool ok = true;
			for (int j: res) if (!dsu.connected(u[j], v[j])) {
				on_tree[i] = j;
				int nxt = count_common_roads(on_tree);
				ok = nxt == cur;
				break;
			}
			if (ok) {
				res.push_back(f);
				res.insert(res.end(), ALL(g));
			}
		}
		// cout << res << '\n';
		// cout << "-----------\n";
		on_tree[i] = f;
	}
	// cout << res << '\n';
	sort(ALL(res));
	res.erase(unique(ALL(res)), res.end());
	return res;
}

#ifdef LOCAL
#include "simurgh.h"

#include <cstdio>
#include <cassert>
#include <vector>
#include <cstdlib>
#include <string>

using namespace std;

static int MAXQ = 30000;

static int n, m, q = 0;
static vector<int> u, v;
static vector<bool> goal;

static void wrong_answer() {
	printf("NO\n");
	exit(0);
}

static bool is_valid(const vector<int>& r) {
	if(int(r.size()) != n - 1)
		return false;

	for(int i = 0; i < n - 1; i++)
		if (r[i] < 0 || r[i] >= m)
			return false;

	return true;
}

static int _count_common_roads_internal(const vector<int>& r) {
	if(!is_valid(r))
		wrong_answer();

	int common = 0;
	for(int i = 0; i < n - 1; i++) {
		bool is_common = goal[r[i]];
		if (is_common)
			common++;
	}

	return common;	
}

int count_common_roads(const vector<int>& r) {
	q++;
	if(q > MAXQ)
		wrong_answer();

	return _count_common_roads_internal(r);
}

int main() {
	file("simurgh");
	assert(2 == scanf("%d %d", &n, &m));

	u.resize(m);
	v.resize(m);

	for(int i = 0; i < m; i++)
		assert(2 == scanf("%d %d", &u[i], &v[i]));

	goal.resize(m, false);

	for(int i = 0; i < n - 1; i++) {
		int id;
		assert(1 == scanf("%d", &id));
		goal[id] = true;
	}

	vector<int> res = find_roads(n, u, v);

	if(_count_common_roads_internal(res) != n - 1)
		wrong_answer();

	printf("YES\n");

	return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 0 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 1 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 1 ms 212 KB correct
11 Correct 0 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 0 ms 212 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 0 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 1 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 1 ms 212 KB correct
11 Correct 0 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 0 ms 212 KB correct
14 Correct 1 ms 212 KB correct
15 Correct 1 ms 212 KB correct
16 Correct 1 ms 212 KB correct
17 Correct 2 ms 212 KB correct
18 Correct 1 ms 212 KB correct
19 Correct 1 ms 212 KB correct
20 Correct 1 ms 212 KB correct
21 Correct 1 ms 212 KB correct
22 Correct 1 ms 212 KB correct
23 Correct 1 ms 212 KB correct
24 Correct 1 ms 212 KB correct
25 Correct 1 ms 212 KB correct
26 Correct 1 ms 212 KB correct
27 Correct 1 ms 212 KB correct
28 Correct 1 ms 212 KB correct
29 Correct 0 ms 212 KB correct
30 Correct 1 ms 212 KB correct
31 Correct 1 ms 212 KB correct
32 Correct 1 ms 212 KB correct
33 Correct 1 ms 212 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 0 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 1 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 1 ms 212 KB correct
11 Correct 0 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 0 ms 212 KB correct
14 Correct 1 ms 212 KB correct
15 Correct 1 ms 212 KB correct
16 Correct 1 ms 212 KB correct
17 Correct 2 ms 212 KB correct
18 Correct 1 ms 212 KB correct
19 Correct 1 ms 212 KB correct
20 Correct 1 ms 212 KB correct
21 Correct 1 ms 212 KB correct
22 Correct 1 ms 212 KB correct
23 Correct 1 ms 212 KB correct
24 Correct 1 ms 212 KB correct
25 Correct 1 ms 212 KB correct
26 Correct 1 ms 212 KB correct
27 Correct 1 ms 212 KB correct
28 Correct 1 ms 212 KB correct
29 Correct 0 ms 212 KB correct
30 Correct 1 ms 212 KB correct
31 Correct 1 ms 212 KB correct
32 Correct 1 ms 212 KB correct
33 Correct 1 ms 212 KB correct
34 Correct 87 ms 980 KB correct
35 Correct 79 ms 1108 KB correct
36 Correct 50 ms 856 KB correct
37 Correct 4 ms 340 KB correct
38 Correct 77 ms 1084 KB correct
39 Correct 71 ms 1048 KB correct
40 Correct 51 ms 856 KB correct
41 Correct 75 ms 1148 KB correct
42 Correct 76 ms 1148 KB correct
43 Correct 38 ms 756 KB correct
44 Correct 31 ms 596 KB correct
45 Correct 37 ms 724 KB correct
46 Correct 27 ms 596 KB correct
47 Correct 12 ms 340 KB correct
48 Correct 2 ms 212 KB correct
49 Correct 4 ms 340 KB correct
50 Correct 12 ms 444 KB correct
51 Correct 35 ms 728 KB correct
52 Correct 46 ms 672 KB correct
53 Correct 29 ms 600 KB correct
54 Correct 38 ms 752 KB correct
55 Correct 41 ms 728 KB correct
56 Correct 34 ms 724 KB correct
57 Correct 37 ms 768 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB correct
2 Correct 1 ms 212 KB correct
3 Incorrect 64 ms 2208 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Correct 0 ms 212 KB correct
4 Correct 0 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 1 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 0 ms 212 KB correct
10 Correct 1 ms 212 KB correct
11 Correct 0 ms 212 KB correct
12 Correct 0 ms 212 KB correct
13 Correct 0 ms 212 KB correct
14 Correct 1 ms 212 KB correct
15 Correct 1 ms 212 KB correct
16 Correct 1 ms 212 KB correct
17 Correct 2 ms 212 KB correct
18 Correct 1 ms 212 KB correct
19 Correct 1 ms 212 KB correct
20 Correct 1 ms 212 KB correct
21 Correct 1 ms 212 KB correct
22 Correct 1 ms 212 KB correct
23 Correct 1 ms 212 KB correct
24 Correct 1 ms 212 KB correct
25 Correct 1 ms 212 KB correct
26 Correct 1 ms 212 KB correct
27 Correct 1 ms 212 KB correct
28 Correct 1 ms 212 KB correct
29 Correct 0 ms 212 KB correct
30 Correct 1 ms 212 KB correct
31 Correct 1 ms 212 KB correct
32 Correct 1 ms 212 KB correct
33 Correct 1 ms 212 KB correct
34 Correct 87 ms 980 KB correct
35 Correct 79 ms 1108 KB correct
36 Correct 50 ms 856 KB correct
37 Correct 4 ms 340 KB correct
38 Correct 77 ms 1084 KB correct
39 Correct 71 ms 1048 KB correct
40 Correct 51 ms 856 KB correct
41 Correct 75 ms 1148 KB correct
42 Correct 76 ms 1148 KB correct
43 Correct 38 ms 756 KB correct
44 Correct 31 ms 596 KB correct
45 Correct 37 ms 724 KB correct
46 Correct 27 ms 596 KB correct
47 Correct 12 ms 340 KB correct
48 Correct 2 ms 212 KB correct
49 Correct 4 ms 340 KB correct
50 Correct 12 ms 444 KB correct
51 Correct 35 ms 728 KB correct
52 Correct 46 ms 672 KB correct
53 Correct 29 ms 600 KB correct
54 Correct 38 ms 752 KB correct
55 Correct 41 ms 728 KB correct
56 Correct 34 ms 724 KB correct
57 Correct 37 ms 768 KB correct
58 Correct 0 ms 212 KB correct
59 Correct 1 ms 212 KB correct
60 Incorrect 64 ms 2208 KB WA in grader: NO
61 Halted 0 ms 0 KB -