107262024-04-10 19:49:22111Útvonalakcpp17Time limit exceeded 45/1001.082s69016 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>{
		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;
					}
				}
			}
			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
1Accepted3ms1828 KiB
2Accepted193ms44472 KiB
subtask215/15
3Accepted4ms2872 KiB
4Accepted4ms3452 KiB
5Accepted8ms6176 KiB
6Accepted14ms6944 KiB
7Accepted75ms23636 KiB
8Accepted162ms44880 KiB
subtask30/15
9Accepted4ms3468 KiB
10Accepted4ms4056 KiB
11Accepted75ms6744 KiB
12Accepted16ms8056 KiB
13Accepted93ms28828 KiB
14Accepted209ms54424 KiB
15Time limit exceeded1.069s54576 KiB
subtask415/15
16Accepted4ms4676 KiB
17Accepted8ms5584 KiB
18Accepted14ms8188 KiB
19Accepted79ms24884 KiB
20Accepted171ms46252 KiB
subtask515/15
21Accepted3ms3964 KiB
22Accepted3ms4060 KiB
23Accepted4ms4980 KiB
24Accepted3ms4160 KiB
25Accepted4ms4636 KiB
26Accepted4ms4848 KiB
subtask60/40
27Accepted3ms4368 KiB
28Accepted170ms46644 KiB
29Accepted4ms2872 KiB
30Accepted4ms3452 KiB
31Accepted8ms6176 KiB
32Accepted14ms6944 KiB
33Accepted75ms23636 KiB
34Accepted162ms44880 KiB
35Accepted4ms3468 KiB
36Accepted4ms4056 KiB
37Accepted75ms6744 KiB
38Accepted16ms8056 KiB
39Accepted93ms28828 KiB
40Accepted209ms54424 KiB
41Time limit exceeded1.069s54576 KiB
42Accepted4ms4676 KiB
43Accepted8ms5584 KiB
44Accepted14ms8188 KiB
45Accepted79ms24884 KiB
46Accepted171ms46252 KiB
47Accepted3ms3964 KiB
48Accepted3ms4060 KiB
49Accepted4ms4980 KiB
50Accepted3ms4160 KiB
51Accepted4ms4636 KiB
52Accepted4ms4848 KiB
53Time limit exceeded1.077s49476 KiB
54Accepted6ms5328 KiB
55Accepted209ms8724 KiB
56Accepted19ms8908 KiB
57Accepted118ms28092 KiB
58Accepted105ms29240 KiB
59Time limit exceeded1.082s69016 KiB
60Accepted256ms51612 KiB
61Accepted263ms52544 KiB
62Accepted252ms53152 KiB