HDU 3118 Arbiter(枚舉)

來源:互聯網
上載者:User

這個題目誰要是在比賽的時候沒出,後來肯定後悔死了

首先題目意思很簡單,就是讓你去掉圖中最少邊數,讓整個圖中任意兩點之間的距離是偶數

也就是圖不存在奇圈

這個很容易想到二部圖,考慮到資料量很小,那麼現在就可以枚舉,怎麼枚舉呢,把整個圖枚舉分成兩部分

然後去除同一集合裡面的連邊,這裡枚舉是用二進位枚舉,0代表一個集合,1代表一個集合,然後遍曆所有

的邊,如果邊的兩個端點在一個集合內,那麼直接去除,選最小的

這裡得到一個教訓 &位元運算的結果是整型,判斷相等要用bool類型強制轉換一下才能得到bool相等

(bool((1<<po[j].x)&i))==(bool((1<<po[j].y)&i))

#include <iostream>#include <string.h>#include <stdio.h>using namespace std;#define maxn 305int n,m;struct point{    int x;    int y;}po[maxn];int main(){    int t;    int i,j,ans,re_ans;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(i=0;i<m;i++)        scanf("%d%d",&po[i].x,&po[i].y);        ans=m;        for(i=0;i< (1<<n);i++)        {            re_ans=0;            for(j=0;j<m;j++)            {                if((bool((1<<po[j].x)&i))==(bool((1<<po[j].y)&i)))                re_ans++;            }            if(ans > re_ans)            ans=re_ans;        }        printf("%d\n",ans);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.