kuangbin專題五並查集 POJ1308 Is It A Tree?(並查集)
題意:
與小希的迷宮一樣。
題解:
參考一下小希的迷宮。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=1e5+7;
int f[MAXN];
bool vis[MAXN];
void init()
{
for(int i=0;i<=1e5;i++)
f[i]=i;
}
int find(int p)
{
while(p!=f[p])
{
f[p]=f[f[p]];
p=f[p];
}
return p;
}
bool Union(int p,int q)
{
int P=find(p);
int Q=find(q);
if(P==Q)
return true;
else
{
f[Q]=P;
}
return false;
}
int main()
{
int k=1;
while(1)
{
int a,b,MAX=0;
init();
memset(vis,false,sizeof(vis));
bool mark=false ,ans=false;
while(~scanf("%d%d",&a,&b))
{
if(a==-1&&b==-1)
{
mark=true;
break;
}
MAX=max(max(MAX,a),b);
vis[a]=true;
vis[b]=true;
if(a==0&&b==0)
break ;
if(Union(a,b))
ans=true;
}
int sum=0;
for(int i=1;i<=MAX;i++)
if(vis[i]&&f[i]==i)
sum++;
if(mark)
break;
if(ans||sum>1)
printf("Case %d is not a tree.\n",k++);
else
printf("Case %d is a tree.\n",k++);
}
}
相關推薦
kuangbin專題五並查集 POJ1308 Is It A Tree?(並查集)
題意: 與小希的迷宮一樣。 題解: 參考一下小希的迷宮。 #include<stdio.h> #include<string.h> #include<algorithm> using namespa
POJ1308 Is It A Tree?【並查集】
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39918 Accepted: 13143 Description A tree is a
NYOJ129 樹的判定 || POJ1308 Is It A Tree? 【並查集應用,樹的定義】
一個有向圖入度為1的節點僅有一個,並且無環,則是一顆樹。 #include<cstdio> #include <cstring> using namespace std; const int MAXSIZE = 10002; int pre[MA
HDU-1325-Is It A Tree?(並查集)
Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directe
hdu 1325 Is It A Tree? (並查集)
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between n
Is It A Tree?(並查集)
題目: 給出一對對的數字a,b,表示從a到b有一條邊。判斷這是不是一棵樹。 多case,每個case以0 0 結尾輸入以-1 -1結尾分析:①每個節點(除了根結點)只有一個入度;②只有一個根結點。 森林:多個根。 入度:指向同一個結點的邊數。 將點逐個加入集合中,然後判
POJ1308 Is It A Tree?(樹,並查集)
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between node
Poj1308 Is It A Tree? 並查集
Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected
POJ - 1308 Is It A Tree?【並查集】
ges sin 分享 ive ostream rep test case 圖片 air A tree is a well-known data structure that is either empty (null, void, nothing) or is a set
【並查集】Is it a tree?
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed e
並查集------M - 小希的迷宮/N - Is It A Tree?
上次Gardon的迷宮城堡小希玩了很久(見Problem B),現在她也想設計一個迷宮讓Gardon來走。但是她設計迷宮的思路不一樣,首先她認為所有的通道都應該是雙向連通的,就是說如果有一個通道連通了房間A和B,那麼既可以通過它從房間A走到房間B,也可以通過它從房間B走到房間A,為了提高難度
hdu 1325/poj 1308 Is It A Tree? 並查集
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between
小希的迷宮 && Is It A Tree?——並查集 / 圖轉化成樹
一、無向圖轉化成樹 (也可用並查集) Description 上次Gardon的迷宮城堡小希玩了很久(見Problem B),現在她也想設計一個迷宮讓Gardon來走。但是她設計迷宮的思路不一樣,首先她認為所有的通道都應該是雙向連通的,就是說如果有一個通道連通了房間A和B
hdu1325 Is It A Tree?並查集
題目地址 單看題目的話和hdu1272是一樣的。但是hdu1272的博文中我也說了,資料比較水,所以我用非並查集的方法就AC了。但是這題的資料沒那麼水,要用到並查集來解。這題的盲點和重點有這麼幾個: 輸入不是以-1 -1結束,而是以兩個負數結束需要用並查集來判斷是不是隻有
hdu 1325/poj 1308 Is It A Tree? 並查集
題目連結 題意:給出n條有向邊,判斷是不是一棵樹。 這題和hdu1272題目類似,只是把無向邊改成了有向邊。多加一個判斷條件,保證子節點是根節點。 #include <iostream>
Codeforces 915F Imbalance Value of a Tree(並查集)
路徑 second long long air bit force 題意 for href 題目鏈接 Imbalance Value of a Tree 題意 給定一棵樹。求樹上所有簡單路徑中的最大權值與最小權值的差值的和。 首先考慮求所有簡單路徑中的最大權值
B - Is It A Tree?
pac size desc long namespace point rec void integer 來源 hdu 1325 A tree is a well-known data structure that is either empty (null, void, n
HUD Is It A Tree?!!!!!)
stdio.h cor nes std case memset null div turn 這個是一個非常惡心的題,剛開始以為是POJ的題,結果發現不是,一直都是超時。上題吧 A tree is a well-known data structure that is eit
Is It A Tree?
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed ed
openjudge1308——Is It A Tree?
題目連結:http://bailian.openjudge.cn/practice/1308?lang=en_US 題目大意:給你一個集合,判斷它是不是一顆樹(無環,點的個數=邊的個數+1,空樹)並查集判斷即可 解釋都在程式碼裡 #include<bits/stdc++.h&