zoukankan      html  css  js  c++  java
  • hihocoder1322 树结构判定(161周)

    hihocoder1322 : 树结构判定(161周)

    题目链接

    思路:

    无向图中判断是不是一棵树。

    • 并查集判断。判断是不是只有一个连通分量。并且该联通分量中没有环。没有环的判定很简单就是看边的数目和顶点数目,如果边数大于等于顶点数则存在环。
    • 也可以用dfs来做。

    ac代码:

    // hiho1322_161week.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<cstring>
    #include<unordered_map>
    
    using namespace std;
    
    int pre[505];
    
    int find(int x)
    {
    	return pre[x] == x ? x : find(pre[x]);
    }
    
    void merge(int x, int y)
    {
    	int xx = find(x);
    	int yy = find(y);
    	if (xx == yy) return;
    	pre[yy] = xx;
    }
    
    int main()
    {
    	int t, n, m;
    	cin >> t;
    	while (t--)
    	{
    		cin >> n >> m;
    		for (int i = 1; i <= n; i++)
    		{
    			pre[i] = i;
    		}
    
    		int x, y;
    		for (int i = 0; i < m; i++)
    		{
    			cin >> x >> y;
    			merge(x, y);
    		}
    
    		int count = 0;
    		for (int i = 1; i <= n; i++)
    		{
    			if (pre[i] == i)
    			{
    				count++;
    			}
    			if (count > 1) break;
    		}
    		if (count == 1 && m < n) cout << "YES" << endl;
    		else cout << "NO" << endl;
    	}
        return 0;
    }
    
    
  • 相关阅读:
    Saltstack cmd.run 多项命令
    salt state.sls windows 传输文件
    mysql 时区更改;5.7 弱口令
    nginx 端口转发
    nohup 后台执行
    检测 nginx 关闭切换keepalived
    Centos 7 安装 dotnet 环境
    unison 双向镜像同步
    samba 配置参数详解
    数据结构与算法面试题80道(15)
  • 原文地址:https://www.cnblogs.com/weedboy/p/7258779.html
Copyright © 2011-2022 走看看