107302024-04-10 20:13:29111Útvonalakcpp17Wrong answer 30/100395ms142716 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,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;
						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
2Accepted173ms44460 KiB
subtask215/15
3Accepted4ms2844 KiB
4Accepted4ms3456 KiB
5Accepted8ms6512 KiB
6Accepted14ms7320 KiB
7Accepted79ms24104 KiB
8Accepted180ms45340 KiB
subtask315/15
9Accepted4ms3948 KiB
10Accepted4ms4400 KiB
11Accepted8ms7036 KiB
12Accepted14ms8232 KiB
13Accepted85ms28296 KiB
14Accepted201ms53840 KiB
15Accepted391ms116528 KiB
subtask40/15
16Accepted6ms4708 KiB
17Wrong answer8ms5888 KiB
18Accepted14ms8632 KiB
19Accepted79ms25700 KiB
20Accepted192ms47064 KiB
subtask50/15
21Accepted3ms4884 KiB
22Wrong answer3ms5004 KiB
23Accepted4ms6064 KiB
24Wrong answer3ms5220 KiB
25Accepted4ms5464 KiB
26Accepted4ms5464 KiB
subtask60/40
27Accepted3ms5204 KiB
28Accepted174ms47592 KiB
29Accepted4ms2844 KiB
30Accepted4ms3456 KiB
31Accepted8ms6512 KiB
32Accepted14ms7320 KiB
33Accepted79ms24104 KiB
34Accepted180ms45340 KiB
35Accepted4ms3948 KiB
36Accepted4ms4400 KiB
37Accepted8ms7036 KiB
38Accepted14ms8232 KiB
39Accepted85ms28296 KiB
40Accepted201ms53840 KiB
41Accepted391ms116528 KiB
42Accepted6ms4708 KiB
43Wrong answer8ms5888 KiB
44Accepted14ms8632 KiB
45Accepted79ms25700 KiB
46Accepted192ms47064 KiB
47Accepted3ms4884 KiB
48Wrong answer3ms5004 KiB
49Accepted4ms6064 KiB
50Wrong answer3ms5220 KiB
51Accepted4ms5464 KiB
52Accepted4ms5464 KiB
53Accepted268ms120156 KiB
54Accepted6ms6148 KiB
55Accepted8ms9680 KiB
56Wrong answer19ms9656 KiB
57Wrong answer119ms28784 KiB
58Wrong answer97ms29960 KiB
59Accepted395ms142716 KiB
60Accepted254ms52396 KiB
61Wrong answer244ms53100 KiB
62Wrong answer250ms53692 KiB