[POI2008]POD-Subdivision of Kingdom

题目描述

Byteasar, king of Byteotia, has finally decided to retire.

He has two sons, however, and is unable to decide which one one of them should succeed him. Therefore he plans to split the kingdom into two halves, making each son a ruler.

After the division, watchtowers have to be built along the roads connecting the halves. Obviously, building them will be costly, so ideally there should be as little roads between the halves as possible.

Luckily, Byteotia consists of an even number of towns, connected by roads. Resulting from the division, each half-kingdom should contain half the towns. Each road connects (directly) two towns.

The roads do not meet nor cross outside towns, though they can lead through flyovers or tunnels. Every two towns are directly connected by at most one road.

Which exact towns should lie in which half of the kingdom is a matter of great importance. You may assume that the land outside the towns can be partitioned in such a way that the roads connecting towns lying in the same half will not cross the border.

On the other hand, one watchtower has to be built by each road connecting towns from different halves.

Task Write a programme that:

reads the descriptions of towns and the roads connecting them from the standard input, determines such a partition of kingdom into halves that both the halves contain an equal number of towns and the number of roads connecting towns lying in different halves is minimum, writes out the result to the standard output.

If more than one optimum partition exists, your programme should pick one of them arbitrarily.

给定一个n个点的无向图,要求将点集分成大小相等的两个子集,使两个子集之间的边数最少

输入输出格式

输入格式:

The first line of the standard input contains two integers and , separated by a single space, denoting the number of towns and number of roads respectively, , , .

The towns are numbered from to .

Each of the following lines contains two integers separated by a single space.

The line no. (for ) contains the numbers and , [](h…

输出格式:

Your programme should write out exactly one line to the standard output.

It should contain integers separated by single spaces.

These should be the numbers of towns belonging to the same half of the kingdom the town no. does, in an increasing order.

输入输出样例

输入样例#1:

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

输出样例#1:

1 2 6


状压 + 搜索

可以先预处理出来选中每个点后会增加哪些边

然后再处理出前2^(n/2)中状态来

既然已经处理出前2^(n/2)的状态了

那不就可以求2^n的状态了吗?

这样就可以直接暴力搜索组合了

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
const int M = 30 ;
const int N = (1 << 14) + 5 ;
using namespace std ;
inline int read() {
    char c = getchar() ; int x = 0 , w = 1 ;
    while(c>'9'||c<'0') { if(c=='-') w = -1 ; c = getchar() ; }
    while(c>='0'&&c<='9') { x = x*10+c-'0' ; c = getchar() ; }
    return x*w ;
}

int n , m ;
int e[M] , Num[M] ;
int Ans = 1e9 , st ;
inline int Count(int x) { return Num[x >> (n >> 1)] + Num[x - ((x >> (n >> 1)) << (n >> 1))] ; }
void Dfs(int t , int k , int tot , int sit1 , int sit2) {
    if(k == (n >> 1)) {
        if(tot < Ans) st = sit1 , Ans = tot ;
        return ;
    }
    for(int i = t ; i <= n ; i ++)
        Dfs(i + 1 , k + 1 , tot + Count((e[i] & (sit2 ^ (1 << (i - 1))))) - Count(e[i] & sit1) , (sit1 | (1 << (i - 1))) , (sit2 ^ (1 << (i - 1)))) ;
    /*
    Count((e[i] & (sit2 ^ (1 << (i - 1)))))相当于把这个点跟所有没选的点都给连接了一条边
    Count(e[i] & sit1) 相当于把这个点跟所有选了的点的边都断掉 
    */
}
int main() {
    n = read() ; m = read() ;
    for(int i = 0 ; i < (1 << (n >> 1)) ; i ++) Num[i] = Num[i >> 1] + (i & 1) ;
    for(int i = 1 , u , v ; i <= m ; i ++) {
        u = read() , v = read() ;
        e[u] |= (1 << (v - 1)) ;
        e[v] |= (1 << (u - 1)) ;
    }
    Dfs(1 , 0 , 0 , 0 , (1 << n) - 1) ;
    for(int i = 1 ; i <= n ; i ++)
        if(st & (1 << (i - 1)))
            printf("%d ",i) ;
    return 0 ;
}

转载于:https://www.cnblogs.com/beretty/p/9724530.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值