Submission #212044

# Submission time Handle Problem Language Result Execution time Memory
212044 2020-03-22T06:59:47 Z abacaba Making Friends on Joitter is Fun (JOI20_joitter2) C++14
0 / 100
32 ms 44920 KB
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
using namespace std;
 
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>

template <typename T> void Min(T &a, T b) {
    a = min(a, b);
}
 
template <typename T> void Max(T &a, T b) {
    a = max(a, b);
}

ostream &operator << (ostream &cout, vector <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}

ostream &operator << (ostream &cout, set <int> &a) {
	cout << "{";
	for (auto & it : a) cout << it << ", ";
	cout << "}";
	return cout;
}

const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 3e5 + 15;
int n, m, p[N], sz[N];

set <int> g[N], gr[N], gr_find[N];

int find(int v) {
	if(v == p[v])
		return v;
	return p[v] = find(p[v]);
}

inline void unio(int a, int b) {
	a = find(a);
	b = find(b);
	if(a != b) {
		if(gr[a].size() < gr[b].size())
			swap(a, b);

		for(int v : gr[b])
			gr[a].insert(v);

		for(int v : gr_find[b])
			gr_find[a].insert(v);

		p[b] = a;
		sz[a] += sz[b];
	}
}

ll ans;

main() {
	for(int i = 0; i < N; ++i)
		p[i] = i, sz[i] = 1;
	ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= m; ++i) {
		int u, v;
		cin >> u >> v;
		if(find(u) == find(v)) {
			cout << ans << endl;
			continue;
		}

		bool flag = false;
		for(int j : gr[find(u)])
			if(find(j) == find(v))
				flag = true;

		if(flag) {
		// if(gr_find[find(u)].count(find(v))) {

			gr[find(u)].erase(v);
			gr_find[find(u)].erase(find(v));

			unio(u, v);
		}
		else {
			gr[find(v)].insert(u);

			gr_find[find(v)].insert(find(u));
		}
		set <int> s = {};
		for(int v = 1; v <= n; ++v)
			s.insert(find(v));

		ans = 0;
		for(int j : s) {
			ans += 1ll * gr[j].size() * sz[j];
			// for(int k : gr[j]) {
			// 	assert(k == find(k));
			// 	ans += sz[k] * sz[j];
			// 	assert(k != j);
			// }
			// for(int k : g[j]) {
			// 	assert(k == find(k));
			// 	ans += sz[k] * sz[j];
			// 	assert(k != j);
			// }
			ans += 1ll * sz[j] * (sz[j] - 1);
		}
		cout << ans << endl;
	}
	return 0;
}

Compilation message

joitter2.cpp:94:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
# Verdict Execution time Memory Grader output
1 Incorrect 32 ms 44920 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 32 ms 44920 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 32 ms 44920 KB Output isn't correct
2 Halted 0 ms 0 KB -