zoukankan      html  css  js  c++  java
  • [洛谷P1330]封锁阳光大学

    目大意:一张无向图,问最少设置几个关键点使得有点被覆盖(一个关键点可以覆盖所有与它相连的点),关键点不可以相邻

    题解:二分图染色,若不冲突则为较少的一种颜色数

    卡点:

    C++ Code:

    #include <cstdio>
    #include <algorithm>
    #include <cstdlib>
    #define maxn 10010
    #define maxm 100010
    int head[maxn], cnt;
    struct Edge {
    	int to, nxt;
    } e[maxm << 1];
    inline void addE(int a, int b) {
    	e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
    }
    
    int n, m, ans;
    int res[2];
    int C[maxn];
    void dfs(int u) {
    	res[C[u]]++;
    	for (int i = head[u]; i; i = e[i].nxt) {
    		int v = e[i].to;
    		if (!~C[v]) {
    			C[v] = C[u] ^ 1;
    			dfs(v);
    		} else if (C[v] != (C[u] ^ 1)) {
    			puts("Impossible");
    			exit(0);
    		}
    	}
    }
    int main() {
    	scanf("%d%d", &n, &m);
    	for (int i = 0, a, b; i < m; i++) {
    		scanf("%d%d", &a, &b);
    		addE(a, b);
    		addE(b, a);
    	}
    	__builtin_memset(C, -1, sizeof C);
    	for (int i = 1; i <= n; i++) if (!~C[i]) {
    		res[0] = res[1] = 0;
    		C[i] = 1;
    		dfs(i);
    		ans += std::min(res[0], res[1]);
    	}
    	printf("%d
    ", ans);
    	return 0;
    }
    

      

  • 相关阅读:
    接口型模式
    spring-cloud-config安全问题
    适配器模式
    spring boot + quartz 集群
    LeetCode Weekly Contest 147
    LeetCode Weekly Contest 146
    LeetCode Weekly Contest 144
    LeetCode Weekly Contest 143
    LeetCode Weekly Contest 142
    LeetCode Weekly Contest 141
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/10013981.html
Copyright © 2011-2022 走看看