107242024-04-10 19:46:20111Útvonalakcpp17Time limit exceeded 45/1001.08s70996 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)->set<int>{
		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)->map<int,int>{
		map<int,int>s;
		for(int x:q[i]){
			s[x]=0;
		}
		for(int j:c[i]){
			if(j==p){
				continue;
			}
			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;
					}
				}
			}
			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
1Accepted3ms1888 KiB
2Accepted204ms47592 KiB
subtask215/15
3Accepted4ms2960 KiB
4Accepted4ms3612 KiB
5Accepted7ms6684 KiB
6Accepted16ms7376 KiB
7Accepted86ms25488 KiB
8Accepted195ms47976 KiB
subtask30/15
9Accepted4ms3720 KiB
10Accepted4ms4232 KiB
11Accepted56ms6760 KiB
12Accepted16ms8016 KiB
13Accepted105ms28688 KiB
14Accepted250ms54640 KiB
15Time limit exceeded1.059s54952 KiB
subtask415/15
16Accepted6ms4132 KiB
17Accepted8ms5332 KiB
18Accepted16ms8012 KiB
19Accepted87ms26452 KiB
20Accepted192ms49232 KiB
subtask515/15
21Accepted3ms4148 KiB
22Accepted3ms4496 KiB
23Accepted4ms5512 KiB
24Accepted3ms4560 KiB
25Accepted4ms4796 KiB
26Accepted4ms4900 KiB
subtask60/40
27Accepted3ms4532 KiB
28Accepted193ms50116 KiB
29Accepted4ms2960 KiB
30Accepted4ms3612 KiB
31Accepted7ms6684 KiB
32Accepted16ms7376 KiB
33Accepted86ms25488 KiB
34Accepted195ms47976 KiB
35Accepted4ms3720 KiB
36Accepted4ms4232 KiB
37Accepted56ms6760 KiB
38Accepted16ms8016 KiB
39Accepted105ms28688 KiB
40Accepted250ms54640 KiB
41Time limit exceeded1.059s54952 KiB
42Accepted6ms4132 KiB
43Accepted8ms5332 KiB
44Accepted16ms8012 KiB
45Accepted87ms26452 KiB
46Accepted192ms49232 KiB
47Accepted3ms4148 KiB
48Accepted3ms4496 KiB
49Accepted4ms5512 KiB
50Accepted3ms4560 KiB
51Accepted4ms4796 KiB
52Accepted4ms4900 KiB
53Time limit exceeded1.08s48508 KiB
54Accepted6ms5652 KiB
55Accepted155ms9092 KiB
56Accepted25ms9880 KiB
57Accepted156ms29748 KiB
58Accepted111ms31336 KiB
59Time limit exceeded1.069s70996 KiB
60Accepted321ms54688 KiB
61Accepted342ms55740 KiB
62Accepted356ms56160 KiB