107252024-04-10 19:47:39111Útvonalakcpp17Time limit exceeded 45/1001.082s73552 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();
					}
					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();
					}
					else{
						t[x]=0;
					}
				}
			}
			for(auto[x,y]:t){
				if(s.count(x)){
					ans[x]=s[x]+y+cc[j].size();
				}
				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
1Accepted3ms2040 KiB
2Accepted194ms47540 KiB
subtask215/15
3Accepted4ms2808 KiB
4Accepted4ms3524 KiB
5Accepted7ms6548 KiB
6Accepted14ms7148 KiB
7Accepted82ms25248 KiB
8Accepted177ms48032 KiB
subtask30/15
9Accepted4ms3768 KiB
10Accepted4ms4488 KiB
11Accepted71ms7456 KiB
12Accepted17ms8664 KiB
13Accepted104ms30492 KiB
14Accepted261ms57248 KiB
15Time limit exceeded1.07s54828 KiB
subtask415/15
16Accepted6ms4944 KiB
17Accepted8ms5624 KiB
18Accepted16ms8120 KiB
19Accepted93ms26728 KiB
20Accepted193ms49492 KiB
subtask515/15
21Accepted3ms4276 KiB
22Accepted3ms4160 KiB
23Accepted6ms5148 KiB
24Accepted3ms4308 KiB
25Accepted4ms4652 KiB
26Accepted4ms4668 KiB
subtask60/40
27Accepted3ms4116 KiB
28Accepted194ms49380 KiB
29Accepted4ms2808 KiB
30Accepted4ms3524 KiB
31Accepted7ms6548 KiB
32Accepted14ms7148 KiB
33Accepted82ms25248 KiB
34Accepted177ms48032 KiB
35Accepted4ms3768 KiB
36Accepted4ms4488 KiB
37Accepted71ms7456 KiB
38Accepted17ms8664 KiB
39Accepted104ms30492 KiB
40Accepted261ms57248 KiB
41Time limit exceeded1.07s54828 KiB
42Accepted6ms4944 KiB
43Accepted8ms5624 KiB
44Accepted16ms8120 KiB
45Accepted93ms26728 KiB
46Accepted193ms49492 KiB
47Accepted3ms4276 KiB
48Accepted3ms4160 KiB
49Accepted6ms5148 KiB
50Accepted3ms4308 KiB
51Accepted4ms4652 KiB
52Accepted4ms4668 KiB
53Time limit exceeded1.054s47724 KiB
54Accepted6ms5152 KiB
55Accepted225ms8424 KiB
56Accepted29ms11072 KiB
57Accepted180ms38040 KiB
58Accepted119ms30928 KiB
59Time limit exceeded1.082s69084 KiB
60Accepted375ms71736 KiB
61Accepted398ms72612 KiB
62Accepted412ms73552 KiB