UVALive 6590
来源:互联网 发布:企业域名是什么意思 编辑:程序博客网 时间:2024/05/20 18:47
题目大意是用26个字母作顶点,开始构建一个给定图的补图,用图描绘一个矩阵。
思路
判环,暴力
首先判断是否有环,如果有则一定可以构建一个20*20的矩阵,否则暴力搜索一个最长路径,使得矩阵的边长为(路径长度 + 1)/ 2
#include <bits/stdc++.h>using namespace std;const int maxn = 100 + 5;int g[maxn][maxn], res;vector<int> Fix;bool vis[maxn];void init(){ memset(g, 0, sizeof(g)); for(int i = 0; i < 26; ++i) for(int j = 0; j < 26; ++j) g[i][j] = 1;}int id, o;bool loop(int u, vector<int> & p){ if(vis[u]){ id = u; o = 1; return true; } vis[u] = true; for(int v = 0; v < 26; ++v) if(g[u][v]){ if(loop(v, p)) { if(o) p.push_back(u); if(u == id) o = 0; return true; } } vis[u] = false; return false;}void dfs(int u, int f, vector<int> & p){ p.push_back(u); for(int v = 0; v < 26; ++v) if(g[u][v] && v != f){ dfs(v, u, p); } if(res < p.size()){ Fix.clear(); res = p.size(); Fix = p; } p.pop_back();}int main(){// freopen("out.txt","w", stdout); int T; scanf("%d", &T); while(T --){ int n; scanf("%d", &n); Fix.clear(); init(); for(int i = 0; i < n; ++i){ char s[3]; scanf("%s", s); int u = s[0] - 'a', v = s[1] - 'a'; g[u][v] = 0; } vector<int> path; bool ok = false; for(int i = 0; i < 26; ++i){ memset(vis, 0, sizeof(vis)); path.clear(); id = o = 0; if(loop(i, path)){ reverse(path.begin(), path.end()); for(int j = 0; j < 20; ++j){ int cnt = j % path.size(); for(int k = 0; k < 20; ++k){ printf("%c", path[cnt++] + 'a'); if(cnt == path.size()) cnt = 0; } printf("\n"); } ok = true; break; } } if(!ok){ res = 0; for(int i = 0; i < 26; ++i){ path.clear(); dfs(i, -1, path); } int len = (Fix.size() + 1) / 2; for(int i = 0; i < len; ++i){ int cnt = i % Fix.size(); for(int k = 0; k < len; ++k){ printf("%c", Fix[cnt++] + 'a'); if(cnt == Fix.size()) cnt = 0; } printf("\n"); } } } return 0;}
0 0
- UVALive 6590
- UVALive 6590 Digraphs
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- 阻塞双端消息队列 BlockingDeque(先进先出的原则管理)
- MYSQL外键(Foreign Key)的创建及使用
- iOS openCV CvMat,Mat和IplImage之间的转化和拷贝
- SE14调整表时提示转换出错,导致数据被清空,怎么恢复
- java中经典算法之一:水仙花数
- UVALive 6590
- charles4.0破解和手机抓包
- 隐藏apk图标
- [转]C++回顾之static与单例模式
- 利用Mongodb的复制集搭建高可用分片,Replica Sets + Sharding的搭建过程
- Palindrome Partitioning
- iOS开发之CocoaPods使用
- Android app 卡顿分析
- 三个空饮料瓶可以换一瓶饮料,给你1箱(50瓶)饮料,你能喝多少瓶饮料?