Submission #328027

# Submission time Handle Problem Language Result Execution time Memory
328027 2020-11-15T09:13:07 Z kshitij_sodani Swapping Cities (APIO20_swap) C++14
37 / 100
2000 ms 33880 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first 
#define b second

#include "swap.h"


int n,m;
int val=0;
vector<pair<int,pair<int,int>>> ed;
int par[100001];
int ss[100001];
int tt[100001];
int dd[100001];
multiset<int> xx[100001];
vector<int> comp[100001];
int ans[100001];

int find(int no){
	if(par[no]==no){
		return no;
	}
	par[no]=find(par[no]);
	return par[no];
}
void upd(int x,int y){
	int stt=0;

	if(ss[x]>tt[x]-1){
		stt=1;
	}
	auto ee=xx[x].end();
	ee--;
	if(*ee>=3){
		stt=1;
	}
	if(stt){
		for(auto j:comp[x]){
			if(ans[j]==-1){
				ans[j]=y;
			}
		}
	}


}
void init(int nn, int mm,vector<int> aa,vector<int> bb,vector<int> cc) {
	m=mm;
	n=nn;
	for(int i=0;i<m;i++){
		ed.pb({cc[i],{aa[i],bb[i]}});
	}
	sort(ed.begin(),ed.end());
	for(int i=0;i<n;i++){
		par[i]=i;
		ss[i]=0;
		dd[i]=0;
		xx[i].clear();
		tt[i]=1;
		xx[i].insert(0);
		ans[i]=-1;
		comp[i].pb(i);
	}
	for(auto j:ed){
		int x=find(j.b.a);
		int y=find(j.b.b);
		if(x==y){
			ss[x]+=1;
			auto jj=xx[x].find(dd[j.b.a]);
			xx[x].erase(jj);
			dd[j.b.a]+=1;
			xx[x].insert(dd[j.b.a]);

			jj=xx[x].find(dd[j.b.b]);
			xx[x].erase(jj);
			dd[j.b.b]+=1;
			xx[x].insert(dd[j.b.b]);
			if(ans[x]==-1){
				upd(x,j.a);
			}
		}
		else{
			if(xx[x].size()<xx[y].size()){
				swap(x,y);
			}
			par[y]=x;
			for(auto jj:xx[y]){
				xx[x].insert(jj);
			}
			for(auto jj:comp[y]){
				comp[x].pb(jj);
				if(ans[x]!=-1 and ans[jj]==-1){
					ans[jj]=j.a;
				}
			}
			xx[y].clear();
			ss[x]+=ss[y]+1;
			tt[x]+=tt[y];
			
			auto jj=xx[x].find(dd[j.b.a]);
			xx[x].erase(jj);
			dd[j.b.a]+=1;
			xx[x].insert(dd[j.b.a]);

			jj=xx[x].find(dd[j.b.b]);
			xx[x].erase(jj);
			dd[j.b.b]+=1;
			xx[x].insert(dd[j.b.b]);
			if(ans[x]==-1){
				upd(x,j.a);
			}
		}
	}
	/*for(int i=0;i<n;i++){
		cout<<ans[i]<<":";
	}
	cout<<endl;*/
}
/*vector<int> adj[100001];
int vis[100001];
int st=1;
void dfs(int no,int par=-1){
	vis[no]=1;
	if(adj[no].size()>2){
		st=0;
	}
	for(auto j:adj[no]){
		if(j==par){
			continue;
		}
		if(vis[j]==0){
			dfs(j,no);
		}
		else{
			st=0;
		}
	}
}*/
int getMinimumFuelCapacity(int aa, int bb) {
	if(ans[aa]==-1 or ans[bb]==-1){
		return -1;
	}
	int tot=max(ans[aa],ans[bb]);
	for(int i=0;i<n;i++){
		par[i]=i;
		ss[i]=0;
		dd[i]=0;
		xx[i].clear();
		tt[i]=1;
		xx[i].insert(0);
		comp[i].clear();
		comp[i].pb(i);
	}
	for(auto j:ed){
		int x=find(j.b.a);
		int y=find(j.b.b);
		if(x==y){
			ss[x]+=1;
			auto jj=xx[x].find(dd[j.b.a]);
			xx[x].erase(jj);
			dd[j.b.a]+=1;
			xx[x].insert(dd[j.b.a]);

			jj=xx[x].find(dd[j.b.b]);
			xx[x].erase(jj);
			dd[j.b.b]+=1;
			xx[x].insert(dd[j.b.b]);
		}
		else{
			if(xx[x].size()<xx[y].size()){
				swap(x,y);
			}
			par[y]=x;
			for(auto jj:xx[y]){
				xx[x].insert(jj);
			}
			for(auto jj:comp[y]){
				comp[x].pb(jj);
			}
			comp[y].clear();
			xx[y].clear();
			ss[x]+=ss[y]+1;
			tt[x]+=tt[y];
			
			auto jj=xx[x].find(dd[j.b.a]);
			xx[x].erase(jj);
			dd[j.b.a]+=1;
			xx[x].insert(dd[j.b.a]);

			jj=xx[x].find(dd[j.b.b]);
			xx[x].erase(jj);
			dd[j.b.b]+=1;
			xx[x].insert(dd[j.b.b]);
		}
		if(find(aa)==find(bb)){
			return max(tot,j.a);
		}
	}








	return -1;
	//return ac;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7532 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7552 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 6 ms 7532 KB Output is correct
6 Correct 6 ms 7532 KB Output is correct
7 Correct 6 ms 7532 KB Output is correct
8 Correct 6 ms 7532 KB Output is correct
9 Correct 253 ms 23648 KB Output is correct
10 Correct 285 ms 26336 KB Output is correct
11 Correct 299 ms 26848 KB Output is correct
12 Correct 325 ms 27360 KB Output is correct
13 Correct 175 ms 25952 KB Output is correct
14 Correct 248 ms 23776 KB Output is correct
15 Correct 382 ms 31596 KB Output is correct
16 Correct 353 ms 30480 KB Output is correct
17 Correct 397 ms 31816 KB Output is correct
18 Correct 240 ms 30152 KB Output is correct
19 Execution timed out 2082 ms 13996 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7532 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Execution timed out 2089 ms 26208 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7428 KB Output is correct
2 Correct 5 ms 7532 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7552 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 6 ms 7532 KB Output is correct
7 Correct 6 ms 7532 KB Output is correct
8 Correct 6 ms 7532 KB Output is correct
9 Correct 6 ms 7532 KB Output is correct
10 Correct 8 ms 7532 KB Output is correct
11 Correct 9 ms 7532 KB Output is correct
12 Correct 9 ms 7532 KB Output is correct
13 Correct 10 ms 7532 KB Output is correct
14 Correct 8 ms 7660 KB Output is correct
15 Correct 8 ms 7532 KB Output is correct
16 Correct 9 ms 7532 KB Output is correct
17 Correct 8 ms 7532 KB Output is correct
18 Correct 9 ms 7532 KB Output is correct
19 Correct 7 ms 7532 KB Output is correct
20 Correct 8 ms 7532 KB Output is correct
21 Correct 7 ms 7532 KB Output is correct
22 Correct 7 ms 7532 KB Output is correct
23 Correct 7 ms 7532 KB Output is correct
24 Correct 8 ms 7660 KB Output is correct
25 Correct 8 ms 7660 KB Output is correct
26 Correct 9 ms 7660 KB Output is correct
27 Correct 7 ms 7532 KB Output is correct
28 Correct 9 ms 7532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7428 KB Output is correct
2 Correct 5 ms 7532 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7552 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 6 ms 7532 KB Output is correct
7 Correct 6 ms 7532 KB Output is correct
8 Correct 6 ms 7532 KB Output is correct
9 Correct 6 ms 7532 KB Output is correct
10 Correct 253 ms 23648 KB Output is correct
11 Correct 285 ms 26336 KB Output is correct
12 Correct 299 ms 26848 KB Output is correct
13 Correct 325 ms 27360 KB Output is correct
14 Correct 175 ms 25952 KB Output is correct
15 Correct 8 ms 7532 KB Output is correct
16 Correct 9 ms 7532 KB Output is correct
17 Correct 9 ms 7532 KB Output is correct
18 Correct 10 ms 7532 KB Output is correct
19 Correct 8 ms 7660 KB Output is correct
20 Correct 8 ms 7532 KB Output is correct
21 Correct 9 ms 7532 KB Output is correct
22 Correct 8 ms 7532 KB Output is correct
23 Correct 9 ms 7532 KB Output is correct
24 Correct 38 ms 9900 KB Output is correct
25 Correct 1496 ms 27208 KB Output is correct
26 Correct 1428 ms 26548 KB Output is correct
27 Correct 1412 ms 26080 KB Output is correct
28 Correct 1395 ms 25696 KB Output is correct
29 Correct 1278 ms 25696 KB Output is correct
30 Correct 1258 ms 24288 KB Output is correct
31 Correct 1520 ms 28000 KB Output is correct
32 Correct 1569 ms 28384 KB Output is correct
33 Correct 625 ms 25156 KB Output is correct
34 Correct 1490 ms 25824 KB Output is correct
35 Correct 7 ms 7532 KB Output is correct
36 Correct 8 ms 7532 KB Output is correct
37 Correct 7 ms 7532 KB Output is correct
38 Correct 7 ms 7532 KB Output is correct
39 Correct 7 ms 7532 KB Output is correct
40 Correct 8 ms 7660 KB Output is correct
41 Correct 8 ms 7660 KB Output is correct
42 Correct 9 ms 7660 KB Output is correct
43 Correct 7 ms 7532 KB Output is correct
44 Correct 9 ms 7532 KB Output is correct
45 Correct 795 ms 24540 KB Output is correct
46 Correct 1449 ms 26848 KB Output is correct
47 Correct 1165 ms 24928 KB Output is correct
48 Correct 927 ms 24416 KB Output is correct
49 Correct 151 ms 17120 KB Output is correct
50 Correct 161 ms 14816 KB Output is correct
51 Correct 617 ms 21728 KB Output is correct
52 Correct 1263 ms 30172 KB Output is correct
53 Correct 731 ms 28932 KB Output is correct
54 Correct 1651 ms 33880 KB Output is correct
55 Correct 726 ms 25312 KB Output is correct
56 Correct 1198 ms 28540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7532 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7552 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 6 ms 7532 KB Output is correct
6 Correct 6 ms 7532 KB Output is correct
7 Correct 6 ms 7532 KB Output is correct
8 Correct 6 ms 7532 KB Output is correct
9 Correct 253 ms 23648 KB Output is correct
10 Correct 285 ms 26336 KB Output is correct
11 Correct 299 ms 26848 KB Output is correct
12 Correct 325 ms 27360 KB Output is correct
13 Correct 175 ms 25952 KB Output is correct
14 Correct 248 ms 23776 KB Output is correct
15 Correct 382 ms 31596 KB Output is correct
16 Correct 353 ms 30480 KB Output is correct
17 Correct 397 ms 31816 KB Output is correct
18 Correct 240 ms 30152 KB Output is correct
19 Execution timed out 2089 ms 26208 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7428 KB Output is correct
2 Correct 5 ms 7532 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7552 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 6 ms 7532 KB Output is correct
7 Correct 6 ms 7532 KB Output is correct
8 Correct 6 ms 7532 KB Output is correct
9 Correct 6 ms 7532 KB Output is correct
10 Correct 253 ms 23648 KB Output is correct
11 Correct 285 ms 26336 KB Output is correct
12 Correct 299 ms 26848 KB Output is correct
13 Correct 325 ms 27360 KB Output is correct
14 Correct 175 ms 25952 KB Output is correct
15 Correct 248 ms 23776 KB Output is correct
16 Correct 382 ms 31596 KB Output is correct
17 Correct 353 ms 30480 KB Output is correct
18 Correct 397 ms 31816 KB Output is correct
19 Correct 240 ms 30152 KB Output is correct
20 Execution timed out 2089 ms 26208 KB Time limit exceeded
21 Halted 0 ms 0 KB -