首页 > 其他分享 >poj-1308

poj-1308

时间:2023-05-23 16:03:23浏览次数:35  
标签:begin set end int 1308 endSetId poj UF


//392K  0MS G++ 
#include <cstdio>
#include <cstring>

using namespace std;

const int MAX = 10000;

int UF_set[MAX];

void UF_get_setId(int curId) {
    int parentId = UF_set[curId];
    if (parentId == 0) {
        return;
    }
    while(UF_set[parentId] != parentId) {
        parentId = UF_set[parentId];
    }
    UF_set[curId] = parentId;
    // printf("UF_get_setId %d -> %d\n", curId, parentId);
}

int caseNum = 1;
int edgeNum;

char UF_fill(int begin, int end) {
    UF_get_setId(begin);
    UF_get_setId(end);
    int beginSetId = UF_set[begin];
    int endSetId = UF_set[end];

    // printf("%d -> %d, %d -> %d\n", begin, beginSetId, end, endSetId);

    // both node not be filled before
    if (!beginSetId && !endSetId) {
        UF_set[begin] = begin;
        UF_set[end] = begin;
    } else if (!beginSetId && endSetId) {
        UF_set[beginSetId] = endSetId;
        UF_set[begin] = endSetId;
    } else if (beginSetId && !endSetId) {
        UF_set[endSetId] = beginSetId;
        UF_set[end] = beginSetId;
    } else {
        if (beginSetId == endSetId) {
            // printf("AAA %d %d %d\n", begin, end, beginSetId);
            return 0;
        } else {
            UF_set[beginSetId] = endSetId;
            UF_set[begin] = endSetId;
        }
    }
    return 1;
}
#define INF 9999999

int minNum = INF;
int maxNum;

char inSameSet() {
    // printf("checkInSameSet %d %d\n", minNum, maxNum);
    int curSetId = 0;
    for (int i = minNum; i <= maxNum; i++) {
        UF_get_setId(i);
        if (!curSetId) {
            curSetId = UF_set[i];
        } else {
            if (UF_set[i] && curSetId != UF_set[i]) {
                // printf("%d %d %d\n", i, curSetId, UF_set[i]);
                return 0;
            }
        }
    }
    return 1;
}

int main() {
    int res = 1;
    memset(UF_set, 0, sizeof(UF_set));
    while(1) {
        int begin, end;
        scanf("%d %d", &begin, &end);
        if (begin == -1 && end == -1) {
            return 0;
        } else if (!begin && !end) {

            if (res && inSameSet()) {
                printf("Case %d is a tree.\n", caseNum);
            } else {
                printf("Case %d is not a tree.\n", caseNum);
            }
            caseNum++;
            res = 1;
            maxNum = 0;
            minNum = INF;
            memset(UF_set, 0, sizeof(UF_set));
        } else {
            maxNum = maxNum > begin ? maxNum : begin;
            maxNum = maxNum > end ? maxNum : end;
            minNum = minNum < begin ? minNum: begin;
            minNum = minNum < end ? minNum: end;
            if (begin == end) {
                res = 0;
            }
            if (res) {
                res = UF_fill(begin, end);
            }
        }
    }
}


上面的code其实有问题的,只不过测试数据弱才AC, 除了考虑并查集来检测回路以外,还有些其他的检测:


标签:begin,set,end,int,1308,endSetId,poj,UF
From: https://blog.51cto.com/u_9420214/6332975

相关文章

  • poj-1120
    //408K16MSG++#include<cstdio>#include<cstring>usingnamespacestd;intdish[20][20];intK[20][20];intD[16];intdays;intgetDensitySum(introwId,intcolumnId){intK=0;K+=dish[rowId][columnId];if(rowId......
  • poj-3641
    //712K0MSG++#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>usingnamespacestd;longlonga,p;//longlongpower2(longlonga,longlongn)//{//longlongret=1;//for(longlongm......
  • poj-1026
    //188K110MSC++#include<cstring>#include<cstdio>#include<iostream>usingnamespacestd;charstr1[205];charstr2[205];intkey[205];intcycleLength[205];//voidreplace(char*str,intkeyLength,intstrLength){//......
  • poj-2707
    //408K0MSG++#include<cstdio>#include<cstring>usingnamespacestd;intoX;intoY;intdX;intdY;inlinedoubleMIN(doublea,doubleb){returna<b?a:b;}inlinedoubleMAX(doublea,doubleb){returna>b?......
  • poj-2635
    //1652K875MSG++1000//1648K1313MSG++10000#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>constintMAX=1000100;charnotPrime[MAX+1];intPrimeNum;intPrimes[MAX];voidcheckPrim......
  • poj-3286
    //stupidmethod!!!!!!!!!!!!!!!//388K360MSG++#include<stdio.h>#include<string.h>#include<math.h>intC[33][33];//C[n][m],choosemfromn;voidgetCombination(){for(intn=0;n<=32;n++){for(intm=......
  • poj-2282
    //380K 32MS G++#include<stdio.h>#include<string.h>#include<math.h>longlongappearTime1[10];longlongappearTime2[10];voidgetAppearTime(intnum,longlong*appearArray){ appearArray[0]=1; if(num==0){ return; }......
  • poj-2249
    //356K16MSG++//356K0MSG++addm==0check//356K16MSG++//356K0MSG++addm==0check#include<stdio.h>#include<string.h>#include<math.h>intm;intn;//voidgetNum(unsignedintn,unsignedintm){//......
  • poj-1037
    //196K16MSC++#include<cstdio>#include<cstring>usingnamespacestd;constintMAX=25;longlongDP[MAX][MAX][2];//0:down.1:upvoidinit(){for(intcurPlankNum=1;curPlankNum<=20;curPlankNum++){for(......
  • poj-2140
    //132K 110MS C++#include<cstring>#include<cstdio>usingnamespacestd;intN;longlongcnt;voidsolve(intN){ intbegin=1; intend=1; longlongsum=1; while(1){ if(begin>N){ break; } //if(begin==......