zoukankan      html  css  js  c++  java
  • 团体程序设计天梯赛 L2-023 图着色问题 (25分)

    题目链接:

    L2-023 图着色问题 (25分)

    思路:

    对于每种方案,我们遍历一遍所有点即可;

    代码:

    #include<bits/stdc++.h>
    
    using namespace std;
    
    const int maxv = 505;
    int v, e, k, n;
    vector<int> G[maxv];
    int c[maxv], vst[maxv];
    bool dfs(int u) {
    	vst[u] = true;
    	for(int & x : G[u]) {
    		if(c[u] == c[x]) return false;
    		if(!vst[x]) return dfs(x);
    	}
    	return true;
    }
    
    int main() {
    #ifdef MyTest
    	freopen("Sakura.txt", "r", stdin);
    #endif
    	ios::sync_with_stdio(false);
    	cin.tie(0);
    	cin >> v >> e >> k;
    	for(int i = 0; i < e; i++) {
    		int x, y;
    		cin >> x >> y;
    		G[x].push_back(y);
    		G[y].push_back(x);	
    	}
    	cin >> n;
    	while(n--) {
    		set<int> st;
    		for(int i = 1; i <= v; i++) {
    			cin >> c[i];
    			st.insert(c[i]);
    			vst[i] = false;
    		}
    		bool res = st.size() == k;
    		for(int i = 1; i <= v; i++) if(!vst[i]) res &= dfs(i);
    		puts(res ? "Yes" : "No");
    	}
    	return 0;
    }
    
  • 相关阅读:
    socket
    netstat
    列表
    突然发现不会写代码了
    算法资源
    bit位操作
    排序算法
    连续子数组最大和
    books
    凸优化
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308657.html
Copyright © 2011-2022 走看看