107282024-04-10 19:52:36111Útvonalakcpp17Wrong answer 0/100384ms137120 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
#ifdef CB
	freopen("be2.txt","r",stdin);
//	freopen("ki.txt","w",stdout);
#endif
	int N,M,K;
	cin>>N>>M>>K;
	vector<vector<int>>g(N+1);
	for(int i=0;i<M;i++){
		int a,b;
		cin>>a>>b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	vector<int>v(N+1),w(N+1),p(N+1),a(N+1);
	vector<vector<int>>c(N+1),cc;
	auto dfs=[&](auto self,int i)->unordered_set<int>{
		unordered_set<int>s;
		w[i]=v[i];
		for(int j:g[i]){
			if(j==p[i]){
				continue;
			}
			if(v[j]){
				if(v[j]<v[i]){
					w[i]=min(w[i],v[j]);
				}
				continue;
			}
			v[j]=v[i]+1;
			p[j]=i;
			auto z=self(self,j);
			if(w[j]>=v[i]){
				a[i]=1;
				z.insert(i);
				for(int k:z){
					c[k].push_back(cc.size());
				}
				cc.emplace_back(z.begin(),z.end());
			}
			else{
				w[i]=min(w[i],w[j]);
				if(s.size()<z.size()){
					swap(s,z);
				}
				s.insert(z.begin(),z.end());
			}
		}
		s.insert(i);
		return s;
	};
	v[1]=1;
	dfs(dfs,1);
	vector<vector<int>>q(N+1);
	for(int i=0;i<K;i++){
		int a,b;
		cin>>a>>b;
		q[a].push_back(i);
		q[b].push_back(i);
	}
	vector<int>ans(K,-1);
	auto dfs2=[&](auto self,int i,int p)->unordered_map<int,int>{
		if(!a[i]){
			exit(1);
		}
		unordered_map<int,int>s;
		for(int x:q[i]){
			s[x]=0;
		}
		for(int j:c[i]){
			if(j==p){
				continue;
			}
			unordered_map<int,int>t;
			for(int k:cc[j]){
				if(k==i||!a[k]){
					continue;
				}
				auto z=self(self,k,j);
				if(t.size()<z.size()){
					swap(t,z);
				}
				for(auto[x,y]:z){
					if(t.count(x)){
						ans[x]=t[x]+y+cc[j].size();
						t.erase(x);
					}
					else{
						t[x]=y;
					}
				}
			}
			for(int k:cc[j]){
				if(k==i||a[k]){
					continue;
				}
				for(int x:q[k]){
					if(t.count(x)){
						ans[x]=t[x]+cc[j].size();
						t.erase(x);
					}
					else{
						t[x]=0;
					}
				}
			}
			if(s.size()<t.size()){
				swap(s,t);
			}
			for(auto[x,y]:t){
				if(s.count(x)){
					ans[x]=s[x]+y+cc[j].size();
					s.erase(x);
				}
				else{
					s[x]=y+cc[j].size()-1;
				}
			}
		}
		return s;
	};
	dfs2(dfs2,1,-1);
	for(int i:ans){
		cout<<i-2<<'\n';
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Wrong answer3ms1828 KiB
2Wrong answer168ms44444 KiB
subtask20/15
3Accepted4ms2828 KiB
4Wrong answer4ms3524 KiB
5Wrong answer7ms6428 KiB
6Accepted14ms6976 KiB
7Accepted75ms23652 KiB
8Wrong answer178ms44672 KiB
subtask30/15
9Wrong answer4ms3392 KiB
10Wrong answer4ms3984 KiB
11Wrong answer8ms6604 KiB
12Wrong answer14ms7692 KiB
13Wrong answer78ms28100 KiB
14Wrong answer199ms53600 KiB
15Wrong answer317ms113340 KiB
subtask40/15
16Wrong answer6ms4556 KiB
17Wrong answer8ms5424 KiB
18Wrong answer16ms8228 KiB
19Wrong answer82ms24988 KiB
20Wrong answer172ms46424 KiB
subtask50/15
21Accepted3ms4496 KiB
22Wrong answer3ms4628 KiB
23Wrong answer4ms5616 KiB
24Wrong answer3ms4776 KiB
25Wrong answer4ms5016 KiB
26Wrong answer4ms5124 KiB
subtask60/40
27Wrong answer3ms4684 KiB
28Wrong answer189ms46840 KiB
29Accepted4ms2828 KiB
30Wrong answer4ms3524 KiB
31Wrong answer7ms6428 KiB
32Accepted14ms6976 KiB
33Accepted75ms23652 KiB
34Wrong answer178ms44672 KiB
35Wrong answer4ms3392 KiB
36Wrong answer4ms3984 KiB
37Wrong answer8ms6604 KiB
38Wrong answer14ms7692 KiB
39Wrong answer78ms28100 KiB
40Wrong answer199ms53600 KiB
41Wrong answer317ms113340 KiB
42Wrong answer6ms4556 KiB
43Wrong answer8ms5424 KiB
44Wrong answer16ms8228 KiB
45Wrong answer82ms24988 KiB
46Wrong answer172ms46424 KiB
47Accepted3ms4496 KiB
48Wrong answer3ms4628 KiB
49Wrong answer4ms5616 KiB
50Wrong answer3ms4776 KiB
51Wrong answer4ms5016 KiB
52Wrong answer4ms5124 KiB
53Wrong answer314ms115764 KiB
54Wrong answer6ms5440 KiB
55Wrong answer8ms8720 KiB
56Accepted19ms9000 KiB
57Wrong answer116ms28268 KiB
58Wrong answer97ms29436 KiB
59Wrong answer384ms137120 KiB
60Wrong answer252ms51776 KiB
61Wrong answer241ms52640 KiB
62Wrong answer275ms53044 KiB