Submission #213403

# Submission time Handle Problem Language Result Execution time Memory
213403 2020-03-25T17:40:53 Z _7_7_ Making Friends on Joitter is Fun (JOI20_joitter2) C++14
100 / 100
1592 ms 119620 KB
#include <bits/stdc++.h>                                           
 
#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
 
 
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first
 
 
using namespace std;
 
 
typedef pair < long long, long long > pll;    
typedef pair < int, int > pii;
typedef unsigned long long ull;         
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;  
typedef long long ll;  
typedef double db;                             
 
 
const int inf = 1e9, maxn = 4e5 + 148, mod = 1e9 + 7, N = 1e5 + 11;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 333;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const db eps = 1e-12, pi = 3.14159265359;
const ll INF = 1e18;



unordered_set < int > in[N], out[N], gc[N], g[N], gr[N];
unordered_map < int, int > cnt1[N];
int p[N], ans, n, m, v, u, cnt[N];
queue < pii > q;
vi comp[N];

int get (int v) {
	return p[v] == v ? v : p[v] = get(p[v]);
}

void merge (int v, int u) {
	v = get(v), u = get(u);

	if (v == u)
		return;

	ans -= cnt1[v][u] * cnt[v];
	ans -= cnt1[u][v] * cnt[u];
	ans += 2 * cnt[v] * cnt[u];

	if (cnt[v] + sz(g[v]) < cnt[u] + sz(g[u]))
		swap(v, u);

	for (auto x : comp[u])
		if (g[v].count(x)) {
			g[v].erase(x);
			gr[x].erase(v);
		}

	ans += sz(g[v]) * cnt[u];

	for (auto x : g[u]) {
		if (get(x) == v)
			continue;

		if (g[v].count(x))
			ans -= cnt[u];//common vertex 
		else {
			g[v].insert(x);	
			gr[x].insert(v);
			gr[x].erase(u);
			++cnt1[v][get(x)];
			ans += cnt[v];
		}

		x = get(x);
		gc[v].insert(x);
		if (gc[x].count(v))
			q.push({v, x});
	}

	for (auto x : comp[u])
		for (auto y : gr[x]) {
			if (y == v)
				continue;
			++cnt1[y][v];
			gc[y].insert(v);
			if (gc[v].count(y))
				q.push({v, y});
		}

	p[u] = v;
	cnt[v] += cnt[u];

	for (auto x : comp[u])
		comp[v].pb(x);		
}                       




main () {
	scanf("%lld%lld", &n, &m);
	for (int i = 1; i <= n; ++i) {
		p[i] = i;
		cnt[i] = 1;
		comp[i].pb(i);
	}

	while (m--) {
		scanf("%lld%lld", &u, &v);
		int V = get(v), U = get(u);
		if (V != U) {
			if (gc[U].count(V)) 
				q.push({V, U});
			else {
				gc[V].insert(U);
				if (!g[V].count(u)) {
					++cnt1[V][U];
					ans += cnt[V];
					g[V].insert(u);
					gr[u].insert(V);
				}
			}
			
			while (sz(q)) {
				merge(q.front().f, q.front().s);
				q.pop();
			}
		}
		printf("%lld\n", ans);
	}
}


Compilation message

joitter2.cpp:111:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main () {
       ^
joitter2.cpp: In function 'int main()':
joitter2.cpp:112:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld%lld", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~~~~
joitter2.cpp:120:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld%lld", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 27 ms 35560 KB Output is correct
2 Correct 27 ms 35584 KB Output is correct
3 Correct 26 ms 35584 KB Output is correct
4 Correct 26 ms 35584 KB Output is correct
5 Correct 26 ms 35584 KB Output is correct
6 Correct 26 ms 35584 KB Output is correct
7 Correct 27 ms 35584 KB Output is correct
8 Correct 27 ms 35584 KB Output is correct
9 Correct 28 ms 35712 KB Output is correct
10 Correct 26 ms 35576 KB Output is correct
11 Correct 26 ms 35584 KB Output is correct
12 Correct 26 ms 35584 KB Output is correct
13 Correct 27 ms 35576 KB Output is correct
14 Correct 27 ms 35584 KB Output is correct
15 Correct 27 ms 35584 KB Output is correct
16 Correct 27 ms 35584 KB Output is correct
17 Correct 26 ms 35584 KB Output is correct
18 Correct 26 ms 35584 KB Output is correct
19 Correct 27 ms 35584 KB Output is correct
20 Correct 26 ms 35584 KB Output is correct
21 Correct 28 ms 35584 KB Output is correct
22 Correct 27 ms 35584 KB Output is correct
23 Correct 26 ms 35584 KB Output is correct
24 Correct 26 ms 35584 KB Output is correct
25 Correct 27 ms 35584 KB Output is correct
26 Correct 26 ms 35584 KB Output is correct
27 Correct 26 ms 35584 KB Output is correct
28 Correct 26 ms 35584 KB Output is correct
29 Correct 27 ms 35584 KB Output is correct
30 Correct 27 ms 35584 KB Output is correct
31 Correct 35 ms 35584 KB Output is correct
32 Correct 26 ms 35584 KB Output is correct
33 Correct 27 ms 35584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 35560 KB Output is correct
2 Correct 27 ms 35584 KB Output is correct
3 Correct 26 ms 35584 KB Output is correct
4 Correct 26 ms 35584 KB Output is correct
5 Correct 26 ms 35584 KB Output is correct
6 Correct 26 ms 35584 KB Output is correct
7 Correct 27 ms 35584 KB Output is correct
8 Correct 27 ms 35584 KB Output is correct
9 Correct 28 ms 35712 KB Output is correct
10 Correct 26 ms 35576 KB Output is correct
11 Correct 26 ms 35584 KB Output is correct
12 Correct 26 ms 35584 KB Output is correct
13 Correct 27 ms 35576 KB Output is correct
14 Correct 27 ms 35584 KB Output is correct
15 Correct 27 ms 35584 KB Output is correct
16 Correct 27 ms 35584 KB Output is correct
17 Correct 26 ms 35584 KB Output is correct
18 Correct 26 ms 35584 KB Output is correct
19 Correct 27 ms 35584 KB Output is correct
20 Correct 26 ms 35584 KB Output is correct
21 Correct 28 ms 35584 KB Output is correct
22 Correct 27 ms 35584 KB Output is correct
23 Correct 26 ms 35584 KB Output is correct
24 Correct 26 ms 35584 KB Output is correct
25 Correct 27 ms 35584 KB Output is correct
26 Correct 26 ms 35584 KB Output is correct
27 Correct 26 ms 35584 KB Output is correct
28 Correct 26 ms 35584 KB Output is correct
29 Correct 27 ms 35584 KB Output is correct
30 Correct 27 ms 35584 KB Output is correct
31 Correct 35 ms 35584 KB Output is correct
32 Correct 26 ms 35584 KB Output is correct
33 Correct 27 ms 35584 KB Output is correct
34 Correct 29 ms 35712 KB Output is correct
35 Correct 148 ms 41976 KB Output is correct
36 Correct 187 ms 45668 KB Output is correct
37 Correct 177 ms 45816 KB Output is correct
38 Correct 171 ms 45304 KB Output is correct
39 Correct 30 ms 36344 KB Output is correct
40 Correct 32 ms 36608 KB Output is correct
41 Correct 33 ms 36480 KB Output is correct
42 Correct 30 ms 36224 KB Output is correct
43 Correct 30 ms 36224 KB Output is correct
44 Correct 30 ms 36224 KB Output is correct
45 Correct 33 ms 36352 KB Output is correct
46 Correct 30 ms 36352 KB Output is correct
47 Correct 33 ms 36472 KB Output is correct
48 Correct 32 ms 36480 KB Output is correct
49 Correct 52 ms 37540 KB Output is correct
50 Correct 185 ms 45996 KB Output is correct
51 Correct 35 ms 36728 KB Output is correct
52 Correct 160 ms 43640 KB Output is correct
53 Correct 40 ms 37292 KB Output is correct
54 Correct 176 ms 44792 KB Output is correct
55 Correct 34 ms 36864 KB Output is correct
56 Correct 34 ms 36864 KB Output is correct
57 Correct 33 ms 36864 KB Output is correct
58 Correct 34 ms 36864 KB Output is correct
59 Correct 31 ms 36224 KB Output is correct
60 Correct 145 ms 41208 KB Output is correct
61 Correct 41 ms 36472 KB Output is correct
62 Correct 173 ms 45044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 35560 KB Output is correct
2 Correct 27 ms 35584 KB Output is correct
3 Correct 26 ms 35584 KB Output is correct
4 Correct 26 ms 35584 KB Output is correct
5 Correct 26 ms 35584 KB Output is correct
6 Correct 26 ms 35584 KB Output is correct
7 Correct 27 ms 35584 KB Output is correct
8 Correct 27 ms 35584 KB Output is correct
9 Correct 28 ms 35712 KB Output is correct
10 Correct 26 ms 35576 KB Output is correct
11 Correct 26 ms 35584 KB Output is correct
12 Correct 26 ms 35584 KB Output is correct
13 Correct 27 ms 35576 KB Output is correct
14 Correct 27 ms 35584 KB Output is correct
15 Correct 27 ms 35584 KB Output is correct
16 Correct 27 ms 35584 KB Output is correct
17 Correct 26 ms 35584 KB Output is correct
18 Correct 26 ms 35584 KB Output is correct
19 Correct 27 ms 35584 KB Output is correct
20 Correct 26 ms 35584 KB Output is correct
21 Correct 28 ms 35584 KB Output is correct
22 Correct 27 ms 35584 KB Output is correct
23 Correct 26 ms 35584 KB Output is correct
24 Correct 26 ms 35584 KB Output is correct
25 Correct 27 ms 35584 KB Output is correct
26 Correct 26 ms 35584 KB Output is correct
27 Correct 26 ms 35584 KB Output is correct
28 Correct 26 ms 35584 KB Output is correct
29 Correct 27 ms 35584 KB Output is correct
30 Correct 27 ms 35584 KB Output is correct
31 Correct 35 ms 35584 KB Output is correct
32 Correct 26 ms 35584 KB Output is correct
33 Correct 27 ms 35584 KB Output is correct
34 Correct 29 ms 35712 KB Output is correct
35 Correct 148 ms 41976 KB Output is correct
36 Correct 187 ms 45668 KB Output is correct
37 Correct 177 ms 45816 KB Output is correct
38 Correct 171 ms 45304 KB Output is correct
39 Correct 30 ms 36344 KB Output is correct
40 Correct 32 ms 36608 KB Output is correct
41 Correct 33 ms 36480 KB Output is correct
42 Correct 30 ms 36224 KB Output is correct
43 Correct 30 ms 36224 KB Output is correct
44 Correct 30 ms 36224 KB Output is correct
45 Correct 33 ms 36352 KB Output is correct
46 Correct 30 ms 36352 KB Output is correct
47 Correct 33 ms 36472 KB Output is correct
48 Correct 32 ms 36480 KB Output is correct
49 Correct 52 ms 37540 KB Output is correct
50 Correct 185 ms 45996 KB Output is correct
51 Correct 35 ms 36728 KB Output is correct
52 Correct 160 ms 43640 KB Output is correct
53 Correct 40 ms 37292 KB Output is correct
54 Correct 176 ms 44792 KB Output is correct
55 Correct 34 ms 36864 KB Output is correct
56 Correct 34 ms 36864 KB Output is correct
57 Correct 33 ms 36864 KB Output is correct
58 Correct 34 ms 36864 KB Output is correct
59 Correct 31 ms 36224 KB Output is correct
60 Correct 145 ms 41208 KB Output is correct
61 Correct 41 ms 36472 KB Output is correct
62 Correct 173 ms 45044 KB Output is correct
63 Correct 781 ms 103600 KB Output is correct
64 Correct 796 ms 103496 KB Output is correct
65 Correct 768 ms 103520 KB Output is correct
66 Correct 361 ms 74592 KB Output is correct
67 Correct 962 ms 94828 KB Output is correct
68 Correct 345 ms 72196 KB Output is correct
69 Correct 419 ms 67680 KB Output is correct
70 Correct 381 ms 73464 KB Output is correct
71 Correct 373 ms 73464 KB Output is correct
72 Correct 768 ms 81276 KB Output is correct
73 Correct 737 ms 82996 KB Output is correct
74 Correct 1592 ms 119620 KB Output is correct
75 Correct 626 ms 79424 KB Output is correct
76 Correct 968 ms 98124 KB Output is correct
77 Correct 974 ms 98380 KB Output is correct
78 Correct 440 ms 75652 KB Output is correct
79 Correct 577 ms 86536 KB Output is correct
80 Correct 289 ms 67400 KB Output is correct
81 Correct 473 ms 74052 KB Output is correct
82 Correct 872 ms 100364 KB Output is correct
83 Correct 871 ms 100436 KB Output is correct
84 Correct 778 ms 100892 KB Output is correct
85 Correct 787 ms 100884 KB Output is correct
86 Correct 440 ms 72420 KB Output is correct
87 Correct 483 ms 74460 KB Output is correct
88 Correct 696 ms 82028 KB Output is correct
89 Correct 925 ms 96536 KB Output is correct