Submission #649851

# Submission time Handle Problem Language Result Execution time Memory
649851 2022-10-11T12:45:05 Z inksamurai Pictionary (COCI18_pictionary) C++17
140 / 140
195 ms 23184 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3yqVz8E ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e

int pvt;

const int _n=100011;

int pns[_n];

struct dsu{
	int _n;
	vi si,par;
	vec(set<int>) qry;
	dsu(int n=0){
		init(n);
	}
	void init(int n=0){
		_n=n;
		par=vi(_n,0);
		si=vi(_n,0);
		qry.clear();
		qry=vec(set<int>)(_n);
		rep(i,n){
			si[i]=1;
			par[i]=i;
		}
	}
	int parent(int u){return par[u]=(par[u]==u?u:parent(par[u]));}
	bool same(int u,int v){return parent(u)==parent(v);}
	void merge(int u,int v){
		u=parent(u),v=parent(v);
		if(!same(u,v)){
			if(si[u]<si[v])swap(u,v);
			vi delay;
			for(auto id:qry[v]){
				if(qry[u].find(id)!=qry[u].end()){
					qry[u].erase(qry[u].find(id));
					pns[id]=pvt;
				}else{
					delay.pb(id);
				}
			}
			for(auto id:delay)qry[u].insert(id);
			_n-=1;
			si[u]+=si[v];
			par[v]=u;
		}
	}
	int size(int v=-1){return v==-1?_n:si[parent(v)];}
};

signed main(){
_3yqVz8E;
	int n,m,q;
	cin>>n>>m>>q;
	dsu uf(n);
	rep(i,q){
		int u,v;
		cin>>u>>v;
		u-=1,v-=1;
		uf.qry[v].insert(i);
		uf.qry[u].insert(i);
	}
	for(int i=m;i>=1;i--){
		pvt=m-i+1;
		for(int j=i;j<=n;j+=i){
			uf.merge(i-1,j-1);
		}
	}
	rep(i,q){
		cout<<(pns[i])<<"\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 4068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 14768 KB Output is correct
2 Correct 111 ms 11188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 188 ms 21396 KB Output is correct
2 Correct 163 ms 17460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 9276 KB Output is correct
2 Correct 71 ms 9616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 10224 KB Output is correct
2 Correct 77 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 14308 KB Output is correct
2 Correct 78 ms 10984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 142 ms 14112 KB Output is correct
2 Correct 150 ms 17868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 172 ms 19740 KB Output is correct
2 Correct 175 ms 20648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 194 ms 22788 KB Output is correct
2 Correct 195 ms 23184 KB Output is correct