POJ3697
阅读原文时间:2023年07月10日阅读:1

/*
Memory Time
7096K 2641MS
*/

#include
#include

using namespace std;

#define HASHLEN 1000117
#define DEMNUM 1000001

int hashTable[HASHLEN];
int factor = ;

int pcs[];

struct Node {
int a;
int b;
int next;
};
Node dam_node[DEMNUM];

int myq[DEMNUM];

inline int is_demaged(int a, int b) {

int idx = hashTable\[(a \* factor + b) % HASHLEN\];  
while (idx > ) {  
    if (dam\_node\[idx\].a == a && dam\_node\[idx\].b == b) {  
        return ;  
    }  
    idx = dam\_node\[idx\].next;  
}  
return ;  

}

int main() {
int case_i = ;
int pc_num = , dam_num = , i, j, num, pca, pcb, head, tail, hVal, idx, flag;
while(scanf("%d %d", &pc_num, &dam_num) && !(pc_num == && dam_num == )) {

    memset(hashTable, 0x00, sizeof(hashTable));  
    memset(pcs, 0x00, sizeof(pcs));  
    for (i = ; i <= dam\_num; i++) {  
        //input data  
        scanf("%d %d", &pca, &pcb);  
        if (pca < pcb) {  
            dam\_node\[i\].a = pca;  
            dam\_node\[i\].b = pcb;  
            hVal = (pca \* factor + pcb) % HASHLEN;  
        } else {  
            dam\_node\[i\].a = pcb;  
            dam\_node\[i\].b = pca;  
            hVal = (pcb \* factor + pca) % HASHLEN;  
        }  
        dam\_node\[i\].next = ;

        //create hash table  
        idx = hashTable\[hVal\];  
        if (idx > ) {  
            dam\_node\[i\].next = idx;  
            hashTable\[hVal\] = i;  
        } else {  
            hashTable\[hVal\] = i;  
        }  
    }

    num = ;  
    head = ;  
    tail = ;  
    for (i = ; i <= pc\_num; i++) {  
        if (!is\_demaged(,i)) {  
            myq\[(tail++) % DEMNUM\] = i;  
            num++;  
            pcs\[i\]=;  
        }  
    }  
    while(head!=tail) {  
        int v = myq\[(head++) % DEMNUM\];  
        for (j = ; j <= pc\_num; j++) {  
            if (pcs\[j\]== || v == j) continue;  
            if (v < j) {  
                flag = is\_demaged(v,j);  
            } else {  
                flag = is\_demaged(j,v);  
            }  
            if (!flag) {  
                pcs\[j\]=;  
                myq\[(tail++) % DEMNUM\] = j;  
                num++;  
            }  
        }  
    }  
    printf("Case %d: %d\\n", ++case\_i, num);  
}  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章