【最大独立集】Graph Coloring

You are to write a program that tries to find an optimal coloring for a given graph. Colors are applied to the nodes of the graph and the only available colors are black and white. The coloring of the graph is called optimal if a maximum of nodes is black. The coloring is restricted by the rule that no two connected nodes may be black.


Figure 1: An optimal graph with three black nodes

Input

The graph is given as a set of nodes denoted by numbers 1…n, n <= 100, and a set of undirected edges denoted by pairs of node numbers (n1, n2), n1 != n2. The input file contains m graphs. The number m is given on the first line. The first line of each graph contains n and k, the number of nodes and the number of edges, respectively. The following k lines contain the edges given by a pair of node numbers, which are separated by a space.

Output

The output should consists of 2m lines, two lines for each graph found in the input file. The first line of should contain the maximum number of nodes that can be colored black in the graph. The second line should contain one possible optimal coloring. It is given by the list of black nodes, separated by a blank.

Sample Input

1
6 8
1 2
1 3
2 4
2 5
3 4
3 6
4 6
5 6

Sample Output

3
1 4 5

继续阅读【最大独立集】Graph Coloring

Regular Bracket Sequence(思维)

A string is called bracket sequence if it does not contain any characters other than “(” and “)”. A bracket sequence is called regular if it it is possible to obtain correct arithmetic expression by inserting characters “+” and “1” into this sequence. For example, “”, “(())” and “()()” are regular bracket sequences; “))” and “)((” are bracket sequences (but not regular ones), and “(a)” and “(1)+(1)” are not bracket sequences at all.
You have a number of strings; each string is a bracket sequence of length 2
. So, overall you have 𝑐𝑛𝑡1 strings “((“, 𝑐𝑛𝑡2 strings “()”, 𝑐𝑛𝑡3 strings “)(” and 𝑐𝑛𝑡4 strings “))”. You want to write all these strings in some order, one after another; after that, you will get a long bracket sequence of length 2(𝑐𝑛𝑡1+𝑐𝑛𝑡2+𝑐𝑛𝑡3+𝑐𝑛𝑡4)
. You wonder: is it possible to choose some order of the strings you have such that you will get a regular bracket sequence? Note that you may not remove any characters or strings, and you may not add anything either. 继续阅读Regular Bracket Sequence(思维)

AC自动机模板

In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.

InputFirst line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters ‘a’-‘z’, and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
OutputPrint how many keywords are contained in the description.Sample Input

1
5
she
he
say
shr
her
yasherhs

Sample Output

3

继续阅读AC自动机模板

【并查集】Wireless Network

传送门

An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B.

In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.

继续阅读【并查集】Wireless Network

字典树模板

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<sstream>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<limits.h>
#define inf 0x7fffffff
#define INFL 0x7fffffffffffffff
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define ULL unsigned long long
using namespace std;

typedef struct Trie{
    int v;
    Trie *next[26];
}Trie;
Trie root;
void createTrie(char *str)
{
    int len = strlen(str);
    Trie *p = &root, *q;
    for(int i=0; i<len; ++i)
    {
        int id = str[i]-'a';
        if(p->next[id] == NULL)
        {
            q = (Trie *)malloc(sizeof(root));
            q->v = 1;
            for(int j=0; j<26; ++j)
                q->next[j] = NULL;
            p->next[id] = q;
            p = p->next[id];
        }
        else
        {
            p->next[id]->v++;
            p = p->next[id];
        }
    }
}

int findTrie(char *str)
{
    int len = strlen(str);
    Trie *p = &root;
    for(int i=0; i<len; ++i)
    {
        int id = str[i]-'a';
        p = p->next[id];
        if(p == NULL)
            return 0;
    }
    return p->v;
}
void dele(char *str,int n)
{
    Trie *p = &root;
    int len = strlen(str);
    for(int i=0; i<len; ++i)
    {
        int id = str[i]-'a';
        p = p->next[id];
        p->v-=n;
    }
    for(int j=0; j<26; ++j)
    {
         p->next[j] = NULL;
    }
}
int main()
{
    int t;
    char s1[10000],s2[10000];
    for(int i=0;i<26;i++)
    {
        root.next[i]=NULL;
    }
    cin>>t;
    while(t--)
    {
        scanf("%s%s",s1,s2);
        if(s1[0]=='i')
        {
            createTrie(s2);
        }
        else if(s1[0]=='s')
        {
            int ans=findTrie(s2);
            if(ans)
            {
                puts("Yes");
            }
            else
            {
                puts("No");
            }
        }
        else
        {
            int cnt=findTrie(s2);
            if(cnt)
            {
                dele(s2,cnt);
            }
        }
    }
    return 0;
}