zoukankan      html  css  js  c++  java
  • Codeforces 27D(二分染色)

    要点

    • 将边作为染色,如果交叉则异色
    #include <cstdio>
    #include <algorithm>
    #include <functional>
    using namespace std;
    
    int n, m;
    int a[101], b[101], c[101];
    
    int main() {
    	scanf("%d %d", &n, &m);
    	for (int i = 1; i <= m; i++) {
    		scanf("%d %d", &a[i], &b[i]);
    		if (a[i] > b[i])
    			swap(a[i], b[i]);
    		c[i] = -1;
    	}
    
    	function<void(int, int)> dfs = [&](int u, int color) {
    		if (c[u] != -1 && c[u] != color) {
    			puts("Impossible"); exit(0);
    		} else if (c[u] == -1) {
    			c[u] = color;
    			for (int i = 1; i <= m; i++) {
    				if (a[i] < a[u] && b[i] > a[u] && b[i] < b[u])
    					dfs(i, color ^ 1);
    				if (a[i] > a[u] && a[i] < b[u] && b[i] > b[u])
    					dfs(i, color ^ 1);
    			}
    		}
    	};
    
    	for (int i = 1; i <= m; i++)
    		if (c[i] < 0)
    			dfs(i, 0);
    	for (int i = 1; i <= m; i++)
    		putchar(c[i] ? 'i' : 'o');
    }
    
  • 相关阅读:
    GO make&new区别
    GO 包相关
    GO 类型断言
    栈 队列 链表
    表达式求值
    动态规划 最长子序列
    04 单例模式
    02 简单工厂模式
    java设计模式 01 开山篇
    java基础07 多线程
  • 原文地址:https://www.cnblogs.com/AlphaWA/p/10997541.html
Copyright © 2011-2022 走看看