Submission #649849

# Submission time Handle Problem Language Result Execution time Memory
649849 2022-10-11T12:38:31 Z inksamurai Pictionary (COCI18_pictionary) C++17
56 / 140
1500 ms 6348 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

struct dsu{
	int _n;
	vi si,par,leb;
	dsu(int n=0){
		init(n);
	}
	void init(int n=0){
		_n=n;
		par=vi(_n,0);
		si=vi(_n,0);
		leb=vi(_n,-1);
		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);
			leb[u]=max(leb[u],leb[v]);
			_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;
	vec(vec(pii)) qry(n);
	rep(i,q){
		int u,v;
		cin>>u>>v;
		u-=1,v-=1;
		qry[v].pb(pii(i,u));
	}
	dsu uf(n);
	vi pns(q,m+1);
	for(int i=m;i>=1;i--){
		for(int j=i;j<=n;j+=i){
			uf.merge(i-1,j-1);
		}
		for(int j=1;j<=n;j++){
			int v=j-1;
			for(auto p:qry[v]){
				int u=p.se;
				if(uf.same(v,u)){
					pns[p.fi]=min(pns[p.fi],m-i+1);
				}
			}
		}
	}
	rep(i,q){
		cout<<(pns[i])<<"\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 266 ms 1692 KB Output is correct
2 Correct 45 ms 2280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 297 ms 2264 KB Output is correct
2 Correct 158 ms 2764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1589 ms 1960 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1590 ms 2496 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1572 ms 3412 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 300 ms 4684 KB Output is correct
2 Execution timed out 1583 ms 5732 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1583 ms 5156 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1587 ms 6348 KB Time limit exceeded
2 Halted 0 ms 0 KB -