蒟蒻WA求救!
查看原帖
蒟蒻WA求救!
1073879
Karl_Wan楼主2024/8/22 18:03

我的思路是基于点的度数:

  1. 如果所有点度数都是2,证明是一个环形结构。
  2. 如果只有2个点度数是1,其余都是2,证明这是一个总线结构。(总线结构的两端度数是1)
  3. 如果有一个点度数是n-1(证明这个点连接了其他所有点),就是一个星型结构。

想不到到底是哪里错了?哪些情况没覆盖到?

谢谢!

#include <iostream>
#include <algorithm>
using namespace std;
int a[100005],n,m;
int main() {
    cin >> n >> m; 
    for (int i = 1; i <= m; i++) {
        int tx,ty;
		cin>>tx>>ty;
		a[tx]++;
		a[ty]++;
    }
    sort(a+1,a+n+1);
    //for(int i=1;i<=n;i++) cout<<a[i]<<' ';
    cout<<endl;
    if(n==2&&m==1)
    {
		cout<<"bus topology";
		return 0;
	}
	if(n==2&&m==2)
	{
		cout<<"unknown topology";
		return 0;
	}
	if(n==1)
	{
		cout<<"unknown topology";
		return 0;
	}
    if(a[1]==1&&a[2]==1&&a[3]==2)
    {
		cout<<"bus topology";
		return 0;
	}
	else if(a[1]==a[n]&&a[n]==2)
	{
		cout<<"ring topology";
		return 0;
	}
	else if(a[n]==n-1&&a[n-1]==1)
	{
		cout<<"star topology";
		return 0;
	}
	cout<<"unknown topology";

    return 0;
}
2024/8/22 18:03
加载中...