zoukankan      html  css  js  c++  java
  • 【luogu P1955 [NOI2015]程序自动分析】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1955

    并查集操作,1e9要离散化,数组要开大一些,操作前先执行合并操作

    样例好毒啊(全是排好序的)

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int maxn = 100010;
    int fa[maxn], n, flag, T, b[maxn];
    struct node{
    	int x, y, e;
    }a[maxn];
    bool cmp(node a, node b)
    {
    	return a.e > b.e;
    }
    int find(int x)
    {
    	return fa[x] == x ? x : fa[x] = find(fa[x]);
    }
    void init(int n)
    {
    	for(int i = 1; i <= n; i++) fa[i] = i;
    }
    int main()
    {
    	scanf("%d",&T);
    	while(T--)
    	{
    		int tot = 0;
    		memset(a, 0, sizeof(a));
    		memset(b, 0, sizeof(b));
    		memset(fa, 0, sizeof(fa));
    		scanf("%d",&n);
    		
    		for(int i = 1; i <= n; i++) 
    		{
    			scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].e);
    			b[++tot] = a[i].x;
    			b[++tot] = a[i].y;
    		}
    		sort(b + 1, b + 1 + tot);
    		int m = unique(b + 1, b + 1 + tot) - b;
    		for(int i = 1; i <= n; i++)
    		{
    			a[i].x = lower_bound(b + 1, b + 1 + m, a[i].x) - b;
    			a[i].y = lower_bound(b + 1, b + 1 + m, a[i].y) - b;
    		}
    		init(m);
    		sort(a+1, a+1+n, cmp);
    		
    		for(int i = 1; i <= n; i++)
    		{
    			flag = 1;
    			int fx = find(a[i].x), fy = find(a[i].y);
    			if(a[i].e == 1)
    			{
    				fa[fx] = fy;
    			}
    			else
    			{
    				if(fx == fy) 
    				{
    					printf("NO
    ");
    					flag = 0;
    					break;
    				}
    			}
    		}
    		if(flag) printf("YES
    ");
    	}
    	return 0;
    }
    
  • 相关阅读:
    HUD--2553 N皇后问题
    poj--2139
    poj--2236
    poj--2229
    poj--2376 Cleaning Shifts
    poj--3669
    poj--1979 Red and Black
    poj--1258
    经典DP问题--poj1088滑雪
    Poj 1041--欧拉回路
  • 原文地址:https://www.cnblogs.com/MisakaAzusa/p/9814529.html
Copyright © 2011-2022 走看看