24h購物| | PChome| 登入
2013-08-23 20:33:48| 人氣2,628| 回應0 | 上一篇 | 下一篇

[UVA] 11065 - A Gentlemen's Agreement

推薦 0 收藏 0 轉貼0 訂閱站台

 A Gentlemen's Agreement

The mayor of Madman City has decided that the city's crossings need newer and better traffic lights. He has commissioned  the company Light's Inc. to remove the old and install the new traffic lights, because it is a very renowned company and one of the leading producers of traffic lights. The people in charge of this task (a mathematician and an engineer) have decided  to minimize the ensuing chaos when the traffic lights are replaced, which entails that not all traffic lights can be replaced simultaneously. On the other hand they do not want to replace the traffic lights one at a time as that would take too long. So they decided that if they replaced the traffic lights at one intersection they would not replace the traffic lights at those intersections which were directly connected to the intersection where the traffic light was being replaced.

        Now the practical engineer was, of course, interested in the maximum number of intersections where the traffic lights could be replaced simultaneously, so he was looking for the largest set of intersections, where no two intersections in the set are connected by a road. The theoretical mathematician however was interested in the number of different sets of intersections in which no subset containing two intersections is connected by a road and it is not possible to add another road without violating the previous condition.

Input and Output

The first line contains the number of cities which are to be processed. The following lines contain the descrition of the cities. A city consists of a number of intersections and of a number of roads which connect the intersections. The number of  intersections i, 1 <= i <= 60 and the number of roads r, 1 <= r <= 3600 are on a line. The intersections are numbered  from 0...i-1. The following r lines contain the description of the roads. The description of a road consists of the two intersections it connects. No intersections will be connected directly by two different roads. Furthermore the graph representing the city's network of roads will be connected.

For each each city, output the number of sets of intersections, where no two intersections in the set are connected by a road and it is not possible to add another intersection to the set without violating the first condition, on the first line. Print the number of intersections in the largest set on the second line.

Sample input

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

Sample output

2
2
3
2
6
4

為了要同時維修紅綠燈,盡可能不維修兩個相鄰的紅綠燈。
問一次最多同時維修多少紅綠燈,以及不可擴充的方案。
(問最大獨立集 S 的 |S| = ? 以及不可擴充增點的獨立集個數)

首先是 DJWS 拿下 Rank 1,我仔細思考了一下,很想隨機化追上去,
但似乎沒那麼簡單。這題一定是 NP-complete problem,甭想了。
點數最多 60 個,看到這數字直接聯想到位元運算,對於所有排斥檢查時使用 XOR 或者是 AND 去做計算,盡可能地減少進位的消耗時間,同時也捨去掉迴圈的 branch,只拿下 Rank 20。



#include <stdio.h>
#include <iostream>
using namespace std;
typedef unsigned long long LL;
unsigned long long g[65];
int hash[10008] = {};
int mx, special, n;
void dfs(LL state, int ch, LL mark, LL noch) {
    //printf("%lld %d\n", state.v, ch);
    if(state == 0) {
        if(mx < ch)
            mx = ch;
        if(mark == (1LL<<n)-1)
            special++;
        return;
    }
    LL lowbit = state&(-state);
    int idx = hash[lowbit%10007];
    //printf("lowbit %lld idx %d\n", lowbit, idx);
    LL tmp = state;
    tmp = tmp ^ (tmp&g[idx]);
    dfs(tmp, ch+1, mark|g[idx], noch);
    tmp = state ^ lowbit;
    dfs(tmp, ch, mark, noch|lowbit);
}
int main() {
    int testcase;
    int m, x, y;
    int i, j, k;
    for(i = 0; i < 63; i++) {
        if(hash[(1LL<<i)%10007])
            puts("ERR");
        hash[(1LL<<i)%10007] = i;
    }
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %d", &n, &m);
        for(i = 0; i < n; i++)
            g[i] = 1LL<<i;
        while(m--) {
            scanf("%d %d", &x, &y);
            g[x] |= 1LL<<y;
            g[y] |= 1LL<<x;
        }
        LL state;
        state = (1LL<<n)-1;
        mx = 1, special = 0;
        dfs(state, 0, 0, 0);
        printf("%d\n%d\n", special, mx);
    }
    return 0;
}

台長: Morris
人氣(2,628) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][math&bitmask] 11471 - Arrange the Tiles
此分類上一篇:[UVA][搜索bitmask] 10318 - Security Panel

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文