107322024-04-10 20:21:32111Útvonalakcpp17Accepted 100/100397ms142520 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("be1.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,int d)->unordered_map<int,int>{
		unordered_map<int,int>s;
		for(int x:q[i]){
			s[x]=d;
		}
		for(int j:c[i]){
			if(j==p){
				continue;
			}
			int dd=d+cc[j].size()-1;
			unordered_map<int,int>t;
			for(int k:cc[j]){
				if(k==i||!a[k]){
					continue;
				}
				auto z=self(self,k,j,dd);
				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()-dd*2;
						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]+1-d;
						t.erase(x);
					}
					else{
						t[x]=dd;
					}
				}
			}
			if(s.size()<t.size()){
				swap(s,t);
			}
			for(auto[x,y]:t){
				if(s.count(x)){
					ans[x]=s[x]+y+1-d*2;
					s.erase(x);
				}
				else{
					s[x]=y;
				}
			}
		}
		return s;
	};
	dfs2(dfs2,1,-1,0);
	for(int i:ans){
		cout<<i-2<<'\n';
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1832 KiB
2Accepted190ms44496 KiB
subtask215/15
3Accepted4ms2748 KiB
4Accepted4ms3464 KiB
5Accepted7ms6232 KiB
6Accepted14ms7140 KiB
7Accepted79ms23972 KiB
8Accepted163ms45212 KiB
subtask315/15
9Accepted4ms3900 KiB
10Accepted4ms4636 KiB
11Accepted8ms7432 KiB
12Accepted14ms8244 KiB
13Accepted79ms28396 KiB
14Accepted173ms54312 KiB
15Accepted321ms116884 KiB
subtask415/15
16Accepted6ms5180 KiB
17Accepted8ms5948 KiB
18Accepted14ms8744 KiB
19Accepted82ms25316 KiB
20Accepted188ms46572 KiB
subtask515/15
21Accepted3ms4496 KiB
22Accepted3ms4404 KiB
23Accepted4ms5500 KiB
24Accepted3ms4660 KiB
25Accepted4ms5068 KiB
26Accepted4ms5180 KiB
subtask640/40
27Accepted3ms4796 KiB
28Accepted171ms47068 KiB
29Accepted4ms2748 KiB
30Accepted4ms3464 KiB
31Accepted7ms6232 KiB
32Accepted14ms7140 KiB
33Accepted79ms23972 KiB
34Accepted163ms45212 KiB
35Accepted4ms3900 KiB
36Accepted4ms4636 KiB
37Accepted8ms7432 KiB
38Accepted14ms8244 KiB
39Accepted79ms28396 KiB
40Accepted173ms54312 KiB
41Accepted321ms116884 KiB
42Accepted6ms5180 KiB
43Accepted8ms5948 KiB
44Accepted14ms8744 KiB
45Accepted82ms25316 KiB
46Accepted188ms46572 KiB
47Accepted3ms4496 KiB
48Accepted3ms4404 KiB
49Accepted4ms5500 KiB
50Accepted3ms4660 KiB
51Accepted4ms5068 KiB
52Accepted4ms5180 KiB
53Accepted324ms120048 KiB
54Accepted6ms5828 KiB
55Accepted8ms9380 KiB
56Accepted19ms9420 KiB
57Accepted116ms28812 KiB
58Accepted103ms29980 KiB
59Accepted397ms142520 KiB
60Accepted233ms52288 KiB
61Accepted256ms53196 KiB
62Accepted264ms53716 KiB