O->O->O (根边点边点)
the first integer identifies the node from which the edge begins
the second integer identifies the node to which the edge is directed.
两相隔的数字是点边点的关系,每个case来的时候,会一直塞点边点,直到0 0出现。
终止input是-1 -1
判断是否有环,除了用adj矩阵配遍历搜寻,还可以用并查集。
用并查集的话,速度比较快,但是输入数字范围只有说大於0,最後找了参考,知道要100000才过。
程序码:
#include <iostream>
#define N 100000
using namespace std;
int used[N];
int arr[N];
int getroot(int);
int main() {
int u, v, num = 1;
while(cin >> u >> v) {
if (u<0 || v<0) break;
int isTree = 1;
for (int i = 0; i < N; ++i) {
arr[i] = i;
used[i] = 0;
}
while(u!=0 && v!=0) {
if (getroot(u) == getroot(v))
isTree = 0;
if (isTree) {
used[u] = used[v] = 1;
arr[getroot(v)] = getroot(u);
}
cin >> u >> v;
}
if (isTree){
int flag = 0;
for(int i = 0; i < N; ++i) {
if (used[i] && getroot(i) == i) {
if (flag) {
isTree = 0;
break;
}
flag = 1;
}
}
}
if (isTree)
cout << "Case " << num << " is a tree." << endl;
else
cout << "Case " << num << " is not a tree." << endl;
num++;
}
return 0;
}
int getroot(int v) {
if (arr[v]!=v){
arr[v] = getroot(arr[v]);
}
return arr[v];
}
参考:
https://web.ntnu.edu.tw/~algo/Tree.html
https://blog.csdn.net/mobius_strip/article/details/39782475
>>: Day3|【Git】终端机常用基本指令 - Mac 作业系统为主
本文将於赛後同步刊登於笔者部落格 有兴趣学习更多 Kubernetes/DevOps/Linux 相...
今天要提到一些关於 GitHub Action 内撰写 YAML 一些技巧,环境变数 (Enviro...
我们可以在变数做宣告时,是否绑定型别来判定程序语言是动态型别或是静态型别,而两者区别如下: 动态型别...
今天是延续昨天的 Hooks 探索,要学习的是 useReducer 、useCallback、us...
今天大概会聊到的范围 @Composable compose compiler & run...