Color Graph

原题链接 https://ac.nowcoder.com/acm/contest/4370/K

去年上海现场赛的一道签到题

太菜了对着这题自闭好久

现在看其实就是一道二分图判断奇环,唯一要思考的地方是怎么枚举可行的情况。解法是因为n很小所以可以二进制暴力枚举染色为1的点然后暴力判断(其实也不难想)

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map> 
#include <stack>
#include <sstream>
#include <set>
#pragma GCC optimize(2)

//#define int long long
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点,确认思路再敲!!! 

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)

const int N = 500, mod = 1e9 + 9, INF = 0x3f3f3f3f;
int t, n, m, idx, id, pos, Max;
int e[N], h[N], ne[N], col[20];
bool flag;

inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

void add(int a, int b) {
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

vector<PII> vec;
int main()
{
    cin >> t;
    while (t--) {
        vec.clear();
        Max = -1;
        mm(h, -1);
        idx = 0;
        cin >> n >> m;
        for (int i = 1; i <= m; ++i) {
            int a, b;
            cin >> a >> b;
            vec.push_back(mp(a, b));
        }
        for (pos = 0; pos <= (1 << n); ++pos) {
            mm(col, 0);
            flag = 1;
            for (int i = 1; i <= n; ++i) {
                if ((pos >> i) & 1 && !col[i]) {
                    col[i] = 1;
                }
            }
            int ans = 0;
            for (int i = 0; i < vec.size(); ++i) {
                int x = vec[i].first, y = vec[i].second;
                if (col[x] != col[y]) ans++;
            }
            Max = max(Max, ans);
            
        }
        printf("Case #%d: %d\n", ++id, Max);
    }
    // system("pause");
    return 0;
}
View Code

虽然现在也很菜,但是去年是真的菜啊......

posted @ 2020-07-07 14:31  violet72  阅读(136)  评论(0编辑  收藏  举报