Submission #321990

# Submission time Handle Problem Language Result Execution time Memory
321990 2020-11-13T16:36:25 Z aZvezda Making Friends on Joitter is Fun (JOI20_joitter2) C++14
1 / 100
5000 ms 18412 KB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
#define out(x) "{" << (#x) << ": " << x << "} "

const int MAX_N = 1e5 + 10;

ll par[MAX_N], sz[MAX_N];
map<pair<ll, ll>, ll> cnt;
set<pair<int, int>> perr[MAX_N];
map<int, int> toClique[MAX_N];
ll ans, in[MAX_N];

int find(int x) {
	if(x == par[x]) {
		return x;
	} else {
		return par[x] = find(par[x]);
	}
}

void remove(int x, int y) {
	ll compx = find(x), compy = find(y);
	if(compx == compy) {return;}
	//cout << "Remove " << x << " " << y << " " << compx << " " << compy  << endl;
	cnt[{compx, compy}] --;
	perr[compx].erase({x, y});
	perr[compy].erase({x, y});
	if(toClique[x][compy] == 1) {
		ans -= sz[compy];
		in[compy] --;
	}
	toClique[x][compy] --;
} 

void add(int x, int y) {
	ll compx = find(x), compy = find(y);
	if(compx == compy) {return;}
	//cout << "Add " << x << " " << y << " " << compx << " " << compy  << endl;
	cnt[{compx, compy}] ++;
 	perr[compx].insert({x, y});
	perr[compy].insert({x, y});
	if(toClique[x][compy] == 0) {
		ans += sz[compy];
		in[compy] ++;
	} 
	toClique[x][compy] ++;
}

stack<pair<int, int> > st;

void merge(int x, int y) {
	ans -= (sz[x] * sz[x] - sz[x]);
	ans -= (sz[y] * sz[y] - sz[y]);
	if(perr[x].size() + sz[x] < perr[y].size() + sz[y]) {swap(x, y);}
	for(auto it : perr[y]) {
		remove(it.first, it.second);
		st.push(it);
	}
	ans += in[x] * sz[y];
	perr[y].clear();
	par[y] = x;
	sz[x] += sz[y]; 
	ans += (sz[x] * sz[x] - sz[x]);
}

void fixStack() {
	//cout << "Fixing" << endl;
	while(!st.empty()) {
		auto curr = st.top(); st.pop();
		//cout << curr << endl;
		if(cnt[{find(curr.second), find(curr.first)}] != 0) {
			//cout << "Have to merge" << curr << endl;
			merge(find(curr.first), find(curr.second));
		} else {
			add(curr.first, curr.second);
		}
	}	
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	for(int i = 0; i < MAX_N; i ++) {par[i] = i; sz[i] = 1;}
	int n, m;
	cin >> n >> m;
	for(int i = 0; i < m; i ++) {
		int a, b;
		cin >> a >> b;
		st.push({a, b});
		fixStack();
		cout << ans << endl;
	}
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 8 ms 11244 KB Output is correct
2 Correct 7 ms 11244 KB Output is correct
3 Correct 7 ms 11244 KB Output is correct
4 Correct 8 ms 11244 KB Output is correct
5 Correct 7 ms 11244 KB Output is correct
6 Correct 8 ms 11244 KB Output is correct
7 Correct 8 ms 11372 KB Output is correct
8 Correct 9 ms 11372 KB Output is correct
9 Correct 8 ms 11372 KB Output is correct
10 Correct 8 ms 11244 KB Output is correct
11 Correct 8 ms 11372 KB Output is correct
12 Correct 7 ms 11372 KB Output is correct
13 Correct 9 ms 11244 KB Output is correct
14 Correct 8 ms 11372 KB Output is correct
15 Correct 8 ms 11372 KB Output is correct
16 Correct 8 ms 11244 KB Output is correct
17 Correct 8 ms 11520 KB Output is correct
18 Correct 8 ms 11372 KB Output is correct
19 Correct 8 ms 11372 KB Output is correct
20 Correct 8 ms 11372 KB Output is correct
21 Correct 9 ms 11372 KB Output is correct
22 Correct 8 ms 11372 KB Output is correct
23 Correct 8 ms 11372 KB Output is correct
24 Correct 9 ms 11372 KB Output is correct
25 Correct 9 ms 11372 KB Output is correct
26 Correct 8 ms 11392 KB Output is correct
27 Correct 8 ms 11392 KB Output is correct
28 Correct 8 ms 11392 KB Output is correct
29 Correct 8 ms 11372 KB Output is correct
30 Correct 8 ms 11372 KB Output is correct
31 Correct 8 ms 11244 KB Output is correct
32 Correct 8 ms 11372 KB Output is correct
33 Correct 8 ms 11372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11244 KB Output is correct
2 Correct 7 ms 11244 KB Output is correct
3 Correct 7 ms 11244 KB Output is correct
4 Correct 8 ms 11244 KB Output is correct
5 Correct 7 ms 11244 KB Output is correct
6 Correct 8 ms 11244 KB Output is correct
7 Correct 8 ms 11372 KB Output is correct
8 Correct 9 ms 11372 KB Output is correct
9 Correct 8 ms 11372 KB Output is correct
10 Correct 8 ms 11244 KB Output is correct
11 Correct 8 ms 11372 KB Output is correct
12 Correct 7 ms 11372 KB Output is correct
13 Correct 9 ms 11244 KB Output is correct
14 Correct 8 ms 11372 KB Output is correct
15 Correct 8 ms 11372 KB Output is correct
16 Correct 8 ms 11244 KB Output is correct
17 Correct 8 ms 11520 KB Output is correct
18 Correct 8 ms 11372 KB Output is correct
19 Correct 8 ms 11372 KB Output is correct
20 Correct 8 ms 11372 KB Output is correct
21 Correct 9 ms 11372 KB Output is correct
22 Correct 8 ms 11372 KB Output is correct
23 Correct 8 ms 11372 KB Output is correct
24 Correct 9 ms 11372 KB Output is correct
25 Correct 9 ms 11372 KB Output is correct
26 Correct 8 ms 11392 KB Output is correct
27 Correct 8 ms 11392 KB Output is correct
28 Correct 8 ms 11392 KB Output is correct
29 Correct 8 ms 11372 KB Output is correct
30 Correct 8 ms 11372 KB Output is correct
31 Correct 8 ms 11244 KB Output is correct
32 Correct 8 ms 11372 KB Output is correct
33 Correct 8 ms 11372 KB Output is correct
34 Correct 11 ms 11500 KB Output is correct
35 Correct 131 ms 18412 KB Output is correct
36 Execution timed out 5098 ms 18412 KB Time limit exceeded
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11244 KB Output is correct
2 Correct 7 ms 11244 KB Output is correct
3 Correct 7 ms 11244 KB Output is correct
4 Correct 8 ms 11244 KB Output is correct
5 Correct 7 ms 11244 KB Output is correct
6 Correct 8 ms 11244 KB Output is correct
7 Correct 8 ms 11372 KB Output is correct
8 Correct 9 ms 11372 KB Output is correct
9 Correct 8 ms 11372 KB Output is correct
10 Correct 8 ms 11244 KB Output is correct
11 Correct 8 ms 11372 KB Output is correct
12 Correct 7 ms 11372 KB Output is correct
13 Correct 9 ms 11244 KB Output is correct
14 Correct 8 ms 11372 KB Output is correct
15 Correct 8 ms 11372 KB Output is correct
16 Correct 8 ms 11244 KB Output is correct
17 Correct 8 ms 11520 KB Output is correct
18 Correct 8 ms 11372 KB Output is correct
19 Correct 8 ms 11372 KB Output is correct
20 Correct 8 ms 11372 KB Output is correct
21 Correct 9 ms 11372 KB Output is correct
22 Correct 8 ms 11372 KB Output is correct
23 Correct 8 ms 11372 KB Output is correct
24 Correct 9 ms 11372 KB Output is correct
25 Correct 9 ms 11372 KB Output is correct
26 Correct 8 ms 11392 KB Output is correct
27 Correct 8 ms 11392 KB Output is correct
28 Correct 8 ms 11392 KB Output is correct
29 Correct 8 ms 11372 KB Output is correct
30 Correct 8 ms 11372 KB Output is correct
31 Correct 8 ms 11244 KB Output is correct
32 Correct 8 ms 11372 KB Output is correct
33 Correct 8 ms 11372 KB Output is correct
34 Correct 11 ms 11500 KB Output is correct
35 Correct 131 ms 18412 KB Output is correct
36 Execution timed out 5098 ms 18412 KB Time limit exceeded
37 Halted 0 ms 0 KB -