图的判环

【1】有向图判环

问题 A: Dwarves
时间限制: 1 Sec 内存限制: 64 MB
提交: 220 解决: 72
[提交] [状态] [讨论版] [命题人:admin]
题目描述

Once upon a time, there arose a huge discussion among the dwarves in Dwarfland. The government wanted to introduce an identity card for all inhabitants.
Most dwarves accept to be small, but they do not like to be measured. Therefore, the government allowed them to substitute the field “height” in their personal identity card with a field “relative dwarf size”. For producing the ID cards, the dwarves were being interviewed about their relative
sizes. For some reason, the government suspects that at least one of the interviewed dwarves must have lied.
Can you help find out if the provided information proves the existence of at least one lying dwarf?

输入

The input consists of:
• one line with an integer n (1 ≤ n ≤ 105 ), where n is the number of statements;
• n lines describing the relations between the dwarves. Each relation is described by:
– one line with “s 1 < s 2 ” or “s 1 > s 2 ”, telling whether dwarf s 1 is smaller or taller than dwarf s 2 . s 1 and s 2 are two different dwarf names.
A dwarf name consists of at most 20 letters from “A” to “Z” and “a” to “z”. A dwarf name does not contain spaces. The number of dwarves does not exceed 104 .

输出

Output “impossible” if the statements are not consistent, otherwise output “possible”.

样例输入

3
Dori > Balin
Balin > Kili
Dori < Kili

样例输出

impossible

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
#define inf 0x7fffffff
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = 100015;
const int M = 24005;
int n,m,cnt=0;
int vis[N];
vector<int>vec[N];
bool flag=false;
map<string,int>p;
void dfs(int x) {
    vis[x]=-1;
    if(flag)return;
    for(int i=0; i<vec[x].size(); i++) {
        int v=vec[x][i];
        if(vis[v]==-1) {
            flag=true;
            return;
        }
        else if(!vis[v])dfs(v);
    }
    vis[x]=1;
}
int main() {
    string a,b,c;
    scanf("%d\n",&n);
    while(n--) {
        cin>>a>>b>>c;
        if(!p[a])p[a]=++cnt;
        if(!p[c])p[c]=++cnt;
        if(b[0]=='>')vec[p[a]].push_back(p[c]);
        else vec[p[c]].push_back(p[a]);
    }
    for(int i=1; i<=cnt; i++) {
        if(flag)break;
        if(!vis[i])dfs(i);
    }
    if(flag)puts("impossible");
    else puts("possible");
    return 0;
}

【2】无向图判环
https://www.cnblogs.com/aiwz/p/6154168.html

https://www.cnblogs.com/ljh2000-jump/p/6049002.html

#include <iostream>  
#include <vector>  
#include <memory.h>  
  
using namespace std;  
  
vector<int> Parent;  
  
void initSet(){  
  
    for(int i = 0; i < Parent.size(); i++)  
        Parent[i] = -1;  
  
}  
  
int findSet(int x){  
  
    if(Parent[x] == -1){  
        return x; // x is root  
    }  
    return findSet(Parent[x]);  
  
}  
  
void UnionSet(int x, int y){  
  
    int xp = findSet(x);  
    int yp = findSet(y);  
    Parent[xp] = yp;  
  
}  
  
int main(){  
  
    int N, E;  
    cin >> N >> E;  
    vector<vector<int> > edges(N);  
    Parent.resize(N);  
    int v1,v2;  
    for(int i = 0; i < E; i++){  
        cin >> v1 >> v2;  
        edges[v1].push_back(v2);  
        //edges[v2].push_back(v1);  
    }  
    // 测试是否有环  
    initSet();  
    for(int v = 0; v < edges.size(); v++){  
        for(int i = 0; i < edges[v].size(); i++){  
            int w = edges[v][i];  
            int xp = findSet(v);  
            int yp = findSet(w);  
            if(xp == yp){  
                cout << "未合并前同根,说明有环。" << endl;  
                return 0;  
            }  
            UnionSet(v,w);  
        }  
    }  
    cout << "无环" << endl;  
    return 0;  
  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值