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 ...
随机推荐
- flask使用debug模式时,存在错误时,会占用设备内存直至服务重启才释放;debug模式会开启一个守护进程(daemon process)
函数调用顺序flask的app.py的run-->werkzeug的serving.py的run_simple-->调用werkzeug的debug的__init__.py里的类Debug ...
- [Javascript] Replicate JavaScript Constructor Inheritance with Simple Objects (OLOO)
Do you get lost when working with functions and the new keyword? Prototypal inheritance can be compl ...
- 怎样去除JSP页面提示:Cannot return from outside a function or method.
今天用myeclipse10写JSP页面时出现: Cannot return from outside a function or method. onClick="return ch ...
- Multiple analytic account plans多辅助核算方案
定义核算方案 菜单 会计/设置/辅助核算会计/多个方案 点击"创建"按钮 说明 辅助核算方案,输入方案名称 点击"添加一个 ...
- cin,和几个get函数的用法
1.cin.get(字符变量名):用来接收字符 ch = cin.get(); cin.get(ch); 以上两者均可以 2.cin.get(字符数组名,接收字符数目)用来接收一行字符串,可以接收空格 ...
- Intel Developer Forum
http://en.wikipedia.org/wiki/Intel_Developer_Forum Intel Developer Forum From Wikipedia, the free en ...
- FastDFS的配置、部署与API使用解读(1)Get Started with FastDFS(转)
转载请注明来自:诗商·柳惊鸿CSDN博客,原文链接:FastDFS的配置.部署与API使用解读(1)入门使用教程 1.背景 FastDFS是一款开源的.分布式文件系统(Distributed File ...
- 使用 C# 开发智能手机软件:推箱子(四)
这是"使用 C# 开发智能手机软件:推箱子"系列文章的第四篇. 在这篇文章中,介绍 Common/FindPath.cs 源程序文件. using System; using Sy ...
- Django-权限信息初始化
数据库 from django.db import models class Menu(models.Model): """ 菜单组: """ ...
- GPG key
Creating GPG Keys - Fedora Project Wiki https://fedoraproject.org/wiki/Creating_GPG_Keys