Poj 1182食物鏈(帶權並查集)

2021-08-19 21:38:59 字數 1512 閱讀 4363

[食物鏈](
time limit: 1000ms memory limit: 10000k

total submissions: 85904 accepted: 25688

description

動物王國中有三類動物a,b,c,這三類動物的食物鏈構成了有趣的環形。a吃b, b吃c,c吃a。

現有n個動物,以1-n編號。每個動物都是a,b,c中的一種,但是我們並不知道它到底是哪一種。

有人用兩種說法對這n個動物所構成的食物鏈關係進行描述:

第一種說法是」1 x y」,表示x和y是同類。

第二種說法是」2 x y」,表示x吃y。

此人對n個動物,用上述兩種說法,一句接一句地說出k句話,這k句話有的是真的,有的是假的。當一句話滿足下列三條之一時,這句話就是假話,否則就是真話。

1) 當前的話與前面的某些真的話衝突,就是假話;

2) 當前的話中x或y比n大,就是假話;

3) 當前的話表示x吃x,就是假話。

你的任務是根據給定的n(1 <= n <= 50,000)和k句話(0 <= k <= 100,000),輸出假話的總數。

input

第一行是兩個整數n和k,以乙個空格分隔。

以下k行每行是三個正整數 d,x,y,兩數之間用乙個空格隔開,其中d表示說法的種類。

若d=1,則表示x和y是同類。

若d=2,則表示x吃y。

output

只有乙個整數,表示假話的數目。

sample input

100 7

1 101 1

2 1 2

2 2 3

2 3 3

1 1 3

2 3 1

1 5 5

sample output

source

noi 01

並查集內所包含的元素,可以不是同一類。

只要2個元素之間有關係,並且這種關係滿足可傳遞性,就可以放入同乙個並查集。

#include 

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using

namespace

std;

const

int n = 60000;

int f[n],w[n];

int find(int x)

return f[x];

}int main()

if(oper==1)

else

}else

else }}

cout

0;}

poj 1182 食物鏈 帶權並查集

這個題需要將動物分成3種,每次以y 0為物件,吃他的x為1,需要注意的是下一次碰到x cha函式遞迴更新與x有關的物件 include include include includeusing namespace std define n 50005 int father n num n int c...

poj1182食物鏈 帶權並查集

基本思路 帶權並查集 簡單的理解就是將有關係的點合併到乙個集合,記錄每個點到集合根節點的權重 include include include define max 50010 using namespace std int par max 記錄集合根節點 int offset max 記錄每個節點到...

poj 1182 食物鏈 帶權並查集

食物鏈 time limit 1000ms memory limit 10000k total submissions 50713 accepted 14818 description 動物王國中有三類動物a,b,c,這三類動物的食物鏈構成了有趣的環形。a吃b,b吃c,c吃a。現有n個動物,以1 n...