UVaLive 6950 && Gym 100299K Digraphs (DFS找环或者是找最长链)
题意:有n个只包含两个字母的字符串, 要求构造一个m*m的字母矩阵, 使得矩阵的每行每列都不包含所给的字符串, m要尽量大,
如果大于20的话构造20*20的矩阵就行了。
析:开始吧,并没有读对题意,后来才看懂什么意思,然后主要思想就是如果有环,那么一定是可以构造成20*20的,只要环一直重复就好,如果没有环,
那么就要找最长的链,然后矩阵长宽就是 (max+1)/2,然后和上面一样构造就好。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 26 + 5;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int G[maxn][maxn];
char s[5];
int ans[maxn], a[maxn];
bool vis[maxn];
int cnt; bool dfs(int u, int num){
bool ok = false;
for(int i = 0; i < 26; ++i){
if(G[u][i]){
a[num] = i;
if(vis[i]){
cnt = num;
return true;
}
else{
vis[i] = true;
if(dfs(i, num+1)) return true;
vis[i] = false;
}
ok = true;
}
}
if(cnt < num && ok){
cnt = num;
memcpy(ans, a, sizeof ans);
}
return false;
} int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
memset(G, -1, sizeof G);
for(int i = 0; i < n; ++i){
scanf("%s", s);
G[s[0]-'a'][s[1]-'a'] = 0;
} int anss = 0;cnt = 0;
for(int i = 0; i < 26; ++i){
memset(vis, false, sizeof vis);
vis[i] = true;
a[0] = i;
if(dfs(i, 1)){
bool ok = false;
int id = 0;
for(int j = 0; j < cnt; ++j)
if(ok) ans[id++] = a[j];
else if(a[j] == a[cnt]){ ans[id++] = a[j]; ok = true; }
anss = id;
break;
}
} if(anss){
for(int i = 0; i < 20; ++i){
for(int j = 0; j < 20; ++j)
printf("%c", ans[(j+i)%anss]+'a');
printf("\n");
}
}
else{
int x = (cnt+2)/2;
for(int i = 0; i < x; ++i){
for(int j = 0; j < x; ++j)
printf("%c", ans[i+j]+'a');
printf("\n");
}
} }
return 0;
}
UVaLive 6950 && Gym 100299K Digraphs (DFS找环或者是找最长链)的更多相关文章
- zstu.4191: 无向图找环(dfs树 + 邻接表)
4191: 无向图找环 Time Limit: 5 Sec Memory Limit: 128 MB Submit: 117 Solved: 34 Description 给你一副无向图,每条边有 ...
- Codeforces Beta Round #88 C. Cycle —— DFS(找环)
题目链接:http://codeforces.com/problemset/problem/117/C C. Cycle time limit per test 2.5 seconds memory ...
- Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂
题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...
- # 「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程)
「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程) 题链 题意:n条边n个节点的连通图,边权为两个节点的权值之和,没有「自环」或「重边」,给出的图中有且只有一个包括奇数个结点的环 ...
- CodeForces 711D Directed Roads (DFS找环+组合数)
<题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...
- 与图论的邂逅06:dfs找环
当我在准备做基环树的题时,经常有了正解的思路确发现不会找环,,,,,,因为我实在太蒻了. 所以我准备梳理一下找环的方法: 有向图 先维护一个栈,把遍历到的节点一个个地入栈.当我们从一个节点x回溯时无非 ...
- HDU - 6370 Werewolf 2018 Multi-University Training Contest 6 (DFS找环)
求确定身份的人的个数. 只能确定狼的身份,因为只能找到谁说了谎.但一个人是否是民,无法确定. 将人视作点,指认关系视作边,有狼边和民边两种边. 确定狼的方法只有两种: 1. 在一个仅由一条狼边组成的环 ...
- 图论 公约数 找环和链 BZOJ [NOI2008 假面舞会]
BZOJ 1064: [Noi2008]假面舞会 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1655 Solved: 798[Submit][S ...
- 洛谷P2444 [POI2000]病毒(AC自动机,DFS求环)
洛谷题目传送门 AC自动机入门--yyb巨佬的博客 AC自动机入手经典好题(虽然年代久远) 有了fail指针,trie树就不是原来的树型结构了,我们可以把它叫做trie图,由父节点向子节点连的边和fa ...
随机推荐
- 2017年记录CS+CV
2017年3月开学,始终感觉自己计算机基础薄弱,加上之前自己也开始对机器学习,深度学习有一些了解,始终感觉没有入门.自己开始规划系统学习计算机软件(CS)和计算机视觉(CV)的基础知识.@2017/9 ...
- PostgreSQL触发器的使用
原文: https://www.yiibai.com/postgresql/postgresql-trigger.html -------------------------------------- ...
- 一种client同步server数据的方案
场景 clientA不定时地把本地数据同步到server上,然后还有一个clientB(app)从server把数据同步下来,汇总展示 clientA数据结构 原始的数据(来自clientA).每条都 ...
- 亲测linux上安装svn
方法一: 1.wget http://subversion.tigris.org/downloads/subversion-1.6.1.tar.gz2.wget http://subversion.t ...
- [UnityShader3]溶解与重现效果
參考链接:http://www.cnblogs.com/Esfog/p/DissolveShader.html 效果图: 从颜色变化来说,有三种,一种是纹理颜色.一种是纹理与黑边的混合颜色,一种是透明 ...
- uboot 对 FAT 分区的解析
uboot 对 FAT 分区的解析 改写 UBOOT 从 U 盘读入固件,然后刷机.发现有的 U 盘无法正确读到分区,跟踪了一下发现自己写的代码有漏洞,只尝试解析分区表里的第一个分区.跟踪的过程中重温 ...
- xcode4中build Settings常见参数解析
本文转载至 http://shiminghua234.blog.163.com/blog/static/263912422012411103526386/ 1.Installation Dir ...
- Cookie 和 Session 的区别 与联系
一. 概念理解 你可能有留意到当你浏览网页时,会有一些推送消息,大多数是你最近留意过的同类东西,比如你想买桌子,上淘宝搜了一下,结果连着几天会有各种各样的桌子的链接.这是因为 你浏览某个网页的时候,W ...
- Kubernetes实战阅读笔记--2、架构和部署
安装Kubernetes “本书准备了4台虚拟机(CentOS 7.0系统)用于部署Kubernetes运行环境,包括一个Etcd.一个Kubernetes Master和三个Kubernetes N ...
- Smoke testing (software)
Smoke testing (software) - Wikipedia https://en.wikipedia.org/wiki/Smoke_testing_(software) In compu ...