Submission #269101

# Submission time Handle Problem Language Result Execution time Memory
269101 2020-08-17T05:09:53 Z 임성재(#5102) Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
7 ms 9728 KB
#include<bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define em emplace
#define eb emplace_back
#define mp make_pair

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INF = 1e18;
const int inf = 1e9;
const ll Mod = 1e9 + 7;

int n, m;
int a[300010];
int b[300010];
int p[100010];
ll sz[100010];
set<int> in[100010], out[100010];
ll ans;

int Find(int a) {
	return a == p[a] ? a : Find(p[a]);
}

void Union(int a, int b) {
	a = Find(a);
	b = Find(b);

	ans -= sz[a] * (sz[a] - 1) + sz[b] * (sz[b] - 1);
	ans -= sz[a] * in[a].size() + sz[b] * in[b].size();

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

	ans += sz[a] * (sz[a] - 1);

	for(auto i : out[b]) {
		if(i != a) {
			out[a].insert(i);
		}
	}

	for(auto i : in[b]) {
		in[a].insert(i);
		out[Find(i)].insert(a);
	}

	vector<int> v;
	for(auto i : in[a]) {
		if(Find(i) == a) v.eb(i);
	}

	for(auto i : v) in[a].erase(i);

	ans += sz[a] * in[a].size();
}

int main() {
	fast;

	cin >> n >> m;

	for(int i=1; i<=n; i++) 
		p[i] = i, sz[i] = 1;

	for(int i=1; i<=m; i++) {
		cin >> a[i] >> b[i];

		if(Find(a[i]) == Find(b[i])) {
			cout << ans << "\n";
			continue;
		}
		if(out[Find(b[i])].find(Find(a[i])) != out[Find(b[i])].end()) {
			Union(a[i], b[i]);
		}
		else {
			ans -= sz[Find(b[i])] * in[Find(b[i])].size();

			out[Find(a[i])].insert(Find(b[i]));
			in[Find(b[i])].insert(a[i]);

			ans += sz[Find(b[i])] * in[Find(b[i])].size();
		}

		cout << ans << "\n";
	}

}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Incorrect 7 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Incorrect 7 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 6 ms 9728 KB Output is correct
3 Incorrect 7 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -