POJ 1308 Is It A Tree? [并查集]

Posted 霜序0.2℃

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 nodes satisfying the following properties.

There is exactly one node, called the root, to which no directed edges point.
Every node except the root has exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.

In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.

Input

The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.

Output

For each test case display the line “Case k is a tree.” or the line “Case k is not a tree.”, where k corresponds to the test case number (they are sequentially numbered starting with 1).

Sample Input

6 8  5 3  5 2  6 4
5 6  0 0

8 1  7 3  6 2  8 9  7 5
7 4  7 8  7 6  0 0

3 8  6 8  6 4
5 3  5 6  5 2  0 0
-1 -1

Sample Output

Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.

解释与代码

并查集经典应用,被收录在kuangbin并查集合集里面,这个是有向的,还有一道无向的,代码差不多,改一改就行,其实就是多判断了一下,入度只能有一个是0

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <stack>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define lbnd        lower_bound
#define ubnd        upper_bound
#define endl        '\\n'
#define mll         map<ll,ll>
#define msl         map<string,ll>
#define mls         map<ll, string>
#define rep(i,a,b)  for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=b-1;i>=a;i--)
#define trav(a, x)  for(auto& a : x)
#define pll         pair<ll,ll>
#define vl          vector<ll>
#define vll         vector<pair<ll, ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define ini2(a,b)   memset(a,b,sizeof(a))
//#define rep(i,a,b)  for(int i=a;i<=b;i++)
#define case        ll T;read(T);for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define pr          printf
#define sc          scanf
//#define _           0
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \\
    (void)(cout << "L" << __LINE__ \\
    << ": " << #x << " = " << (x) << '\\n')
#define TIE \\
    cin.tie(0);cout.tie(0);\\
    ios::sync_with_stdio(false);
//#define long long int

//using namespace __gnu_pbds;

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI    = acos(-1.0);
const double eps   = 1e-6;
const int    INF   = 0x3f3f3f3f;
const ll     LLINF = 0x3f3f3f3f3f3f3f3f;

template <typename T>
void read(T &x) {
    x = 0;
    int f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch ^ 48);
        ch = getchar();
    }
    x *= f;
    return;
}

inline void write(ll x) {
    if(x<0) putchar('-'), x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}

const ll     LN    = 5;
const int    maxn  = 100009;
const int    N     = 1050;

int fa[maxn];
int ax[maxn];
set<int> se;
int in[maxn];
int cntx = 0;

int get(int x) {
	if (x == fa[x]) return x;
	return fa[x] = get(fa[x]);
}

void init() {
	cntx = 0;
	for (int i=0; i<maxn; i++) fa[i] = i, ax[i] = -1, in[i] = 0;
	se.clear();
}

void solve() {
	int x, y;
	int cas = 0;
	while (cin>>x>>y) {
		if (x == 0 && y == 0) {
			cout<<"Case " << ++cas << " is a tree.\\n";
			continue;
		}
		if (x == -1 && y == -1) break;
		init();
		in[y]++;
		int ans = 1;
		if (x == y) {
			if (se.count(x) == 0)
			ax[cntx++] = x, se.insert(x);
			ans = 0;
		} else {
			if (se.count(x) == 0) {
				ax[cntx++] = x;
				se.insert(x);
			}
			if (se.count(y) == 0) {
				ax[cntx++] = y;
				se.insert(y);
			}
		}
		x = get(x), y = get(y);
		fa[x] = y;
		while (cin>>x>>y) {
			if (x == 0 && y == 0) break;
			in[y]++;
			if (x == y) {
				if (se.count(x) == 0)
				ax[cntx++] = x, se.insert(x);
				ans = 0;
			} else {
				if (se.count(x) == 0) {
					ax[cntx++] = x;
					se.insert(x);
				}
				if (se.count(y) == 0) {
					ax[cntx++] = y;
					se.insert(y);
				}
			}
			x = get(x), y = get(y);
			if (x == y) ans = 0;
			fa[x] = y;
		}
		int q = get(ax[0]);
		for (int i=1; i<cntx; i++) {
			if (get(ax[i]) != q) {
				ans = 0;
				break;
			}
		}
//		for (int i=0; i<cntx; i++) {
//			cout<<ax[i]<<endl;
//		}
		if (ans == 1) {
			int in0 = 0;
			for (int i=0; i<cntx; i++) {
				if (in[ax[i]] == 0) in0++;
			}
			if (in0 == 1) {
				cout<<"Case " << ++cas << " is a tree.\\n";
			} else {
				cout<<"Case " << ++cas << " is not a tree.\\n";
			}
		}
		else cout<<"Case " << ++cas << " is not a tree.\\n";
	}
	
}

int main()
{
//	TIE;
//    #ifndef ONLINE_JUDGE
//    freopen ("input.txt","r",stdin);
//    #else
//    #endif
	solve();
//    case{solve();}
//    case{cout<<"Case "<<Q<<":"<<endl;solve();}
//	return ~~(0^_^0);
}

以上是关于POJ 1308 Is It A Tree? [并查集]的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1325,POJ 1308 Is It A Tree

POJ1308 Is It A Tree?

POJ - 1308 Is It A Tree?并查集

POJ 1308 Is It A Tree? [并查集]

POJ 1308 Is It A Tree?

POJ 1308 Is It A Tree?--题解报告