bzoj 4878 [Lydsy2017年5月月赛]挑战NP-Hard(dfs)
来源:互联网 发布:魔神召唤士 网络骑士 编辑:程序博客网 时间:2024/06/10 16:44
题意:
问题1:给定无向图G和一个正整数k。对于图中的每个点,选择一个在[1,k]之间的整数作为其颜色。你需要保证对于每条边,其两端点的颜色均不相同。
问题2:给定无向图G和一个正整数k。请找到一条经过了恰好k条边的简单路径。
你只需解决上面的任意一个问题即可。
思路:说是无向图,就要注意孤点的情况。易wa。构建图的dfs树,然后你会发现,如果深度不足,就一定可以染色,深度足,一定可以找到路径,所以dfs求最大深度即可,我一开始写了一个求图的直径的。发现没必要,因为非1必2啊。
#include <bits/stdc++.h>using namespace std;const int maxn = 1000 + 5;int n, m, k;int root, dep, pa[maxn], d[maxn];vector<int>G[maxn];void dfs(int cur, int fa, int lv){ if(lv > dep) dep = lv, root = cur; d[cur] = lv; pa[cur] = fa; for(int i = 0; i < G[cur].size(); i++) { int o = G[cur][i]; if(d[o] == 0) dfs(o, cur, lv + 1); }}int main(){ int T; scanf("%d", &T); while(T--) { scanf("%d%d%d", &n, &m, &k); for(int i = 1; i <= n; i++) G[i].clear(), d[i] = 0; for(int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); G[x].push_back(y); G[y].push_back(x); } root = 1, dep = 0; for(int i = 1; i <= n; i++) if(d[i] == 0) dfs(i, -1, 1); if(dep >= k + 1) { printf("path"); for(int now = root, cnt = 1; now != -1 && cnt <= k + 1; cnt++) { printf(" %d", now); now = pa[now]; } } else { printf("color"); for(int i = 1; i <= n; i++) printf(" %d", d[i]); } puts(""); } return 0;}
阅读全文
0 0
- bzoj 4878 [Lydsy2017年5月月赛]挑战NP-Hard(dfs)
- bzoj 4878: [Lydsy2017年5月月赛]挑战NP-Hard dfs
- 4878: [Lydsy2017年5月月赛]挑战NP-Hard
- BZOJ 4878([Lydsy2017年5月月赛]挑战NP-Hard-染色问题)
- bzoj4878: [Lydsy2017年5月月赛]挑战NP-Hard
- BZOJ4878: [Lydsy2017年5月月赛]挑战NP-Hard
- bzoj 4880: [Lydsy2017年5月月赛]排名的战争
- bzoj 4884 [Lydsy2017年5月月赛]太空猫(简单dp)
- bzoj4885: [Lydsy2017年5月月赛]长方体
- BZOJ4885: [Lydsy2017年5月月赛]长方体
- bzoj5049 [Lydsy2017年5月月赛]导航系统
- BZOJ 4801([Lydsy2017年4月月赛]打牌-分类讨论)
- [BZOJ]4832: [Lydsy2017年4月月赛]抵制克苏恩
- bzoj 4831: [Lydsy2017年4月月赛]序列操作
- [bzoj-4832][Lydsy2017年4月月赛]抵制克苏恩 题解
- [BZOJ 4879][Lydsy2017年5月月赛]失控的数位板:模拟
- [BZOJ 4883][Lydsy2017年5月月赛]棋盘上的守卫:最小生成树
- bzoj 4883: [Lydsy2017年5月月赛]棋盘上的守卫 最小生成树
- 关于web.xml中<web-app>报错解决方案
- 写会MySQL索引
- HTML(一)
- springboot系列(第四讲)
- Retrofit
- bzoj 4878 [Lydsy2017年5月月赛]挑战NP-Hard(dfs)
- leetcode 648. Replace Words
- The computation of homography, essential and fundamental matrix
- Git初次使用
- day01笔记
- 数独游戏
- vijos 夜夜的noip之旅 t1
- 计算并输出1+2+...+n的值
- day02笔记