章鱼子图,数环,记顶点

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
//#define int long long

using namespace std;
vector<int>g[110],f(110),s(110),d(110);
bool v[110];

int find(int x){
	if(x!=f[x]) 
	return find(f[x]);
	return f[x];
	
}
//dfs不对,要求最短距离,不能回头,要有d数组记录
int bfs(int st,int ed){
	queue<int>q;
	q.push(st);
	v[st]=1;
	int t;
	while(q.size()) {
		t=q.front();
		q.pop();
		for(int i=0;i<g[t].size();i++){
			int x=g[t][i];
			if(v[x]||x==t||x==ed) continue;
			v[x]=1;
			d[x]=d[t]+1;
			q.push(x);
		}
	}
	d[ed]=d[t]+1;
	return d[ed];
	
}

int main(){
	
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	int t;
	cin>>t;
	
	while(t--){
		int l,r;
		int n,m;
		int a,b;
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			g[i].clear();
			v[i]=0,s[i]=0,f[i]=i,d[i]=0;
		}
		for(int i=0;i<m;i++){
			cin>>a>>b;
			g[a].push_back(b);
			g[b].push_back(a);
			
			int fa=find(a);
			int fb=find(b);
			
			if(fa==fb) s[fb]++,l=a,r=b;
			else{
				s[fb]+=s[fa];
				f[fa]=fb;
			}
		}
		
		int ans=0;
		for(int i=1;i<=n;i++){
			if(find(i)==i&&s[i]==1) ans++;
		}
		
		if(ans!=1){
			cout<<"No "<<ans<<endl;
		}else{
			cout<<"Yes "<<bfs(l,r)+1<<endl;
		}
	}
	
	return 0;
}

睿抗编程技能赛2024某题。

(kt) jia@jia-kt:~/autod1-tmp/ktransformers$ gdb ./ktransformers/local_chat.py gdb: /home/jia/anaconda3/envs/kt/lib/libtinfo.so.6: no version information available (required by gdb) gdb: /home/jia/anaconda3/envs/kt/lib/libncursesw.so.6: no version information available (required by gdb) gdb: /home/jia/anaconda3/envs/kt/lib/libncursesw.so.6: no version information available (required by gdb) gdb: /home/jia/anaconda3/envs/kt/lib/libncursesw.so.6: no version information available (required by gdb) GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2) 12.1 Copyright (C) 2022 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <https://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... "/home/jia/autod1-tmp/ktransformers/./ktransformers/local_chat.py": not in executable format: file format not recognized (gdb) core-file /home/jia/autod1-tmp/ktransformers/core.144757 warning: Can't open file /dev/zero (deleted) during file-backed mapping note processing [New LWP 145157] [New LWP 144757] [New LWP 144758] [New LWP 144762] [New LWP 144760] [New LWP 144768] [New LWP 144765] [New LWP 144766] [New LWP 144772] [New LWP 144763] [New LWP 144764] [New LWP 144771] [New LWP 144774] [New LWP 144769] [New LWP 144770] [New LWP 144767] [New LWP 144776] [New LWP 144775] [New LWP 144773] [New LWP 144777] [New LWP 145156] [New LWP 144779] [New LWP 144759] [New LWP 145155] [New LWP 145160] [New LWP 145161] [New LWP 144792] [New LWP 144761] [New LWP 145158] [New LWP 145159] [New LWP 144784] [New LWP 144778] [New LWP 145162] [New LWP 145170] [New LWP 145166] [New LWP 144782] [New LWP 145174] [New LWP 144785] [New LWP 145176] [New LWP 144788] [New LWP 144787] [New LWP 145168] [New LWP 145163] [New LWP 144781] [New LWP 144780] [New LWP 145177] [New LWP 145173] [New LWP 145164] [New LWP 144786] [New LWP 145171] [New LWP 145178] [New LWP 145165] [New LWP 145179] [New LWP 145169] [New LWP 145172] [New LWP 145175] [New LWP 144783] [New LWP 145167] [New LWP 145180] warning: Section `.reg-xstate/145157' in core file too small. --Type <RET> for more, q to quit, c to continue without paging--
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值