【bzoj4065】Graphic Madness
来源:互联网 发布:阿里云 地区选择 编辑:程序博客网 时间:2024/05/20 00:51
CERC2012的(非超级大水题)最后一题了……
这题没点智商似乎真的不会做呢……
就是求一个特殊图的哈密顿回路,只是这个图由两棵树构成,两棵树之间的叶子节点数相等且一一有连边。
一开始我想,是否某个点的度数大于某个值之后就一定无解呢
然后我构造了一个模型把这个想法否掉了。
但是换一个姿势,如果是和叶子节点链接的个数呢?
注意到每个叶子节点的度数为2,这意味着经过叶子节点的时候其父亲节点一定会被顺带经过一下。
这意味着什么?
意味着,它的父亲最多只能有两个“有效儿子”和它相邻,否则第三个节点的出现必定会导致其再走多一次。
知道了这个之后又该怎么办呢?
知道了这个之后,就可以把那些链接着两个以上节点的边给删掉。也就是说,dfs一次,然后先处理了底层,判断边删不删,然后再处理当前层。
于是不该要的边都不要了。
剩下的边又是不是一定要呢?
似乎根据哈密顿回路的必要条件,直接跑个dfs暴力判一判就可以了。
时间复杂度
#include <bits/stdc++.h>using namespace std;#define rep(i,a,b) for(int i = a , _ = b ; i <= _ ; i ++)#define per(i,a,b) for(int i = a , _ = b ; i >= _ ; i --)#define fore(i,u) for(int i = head[u] ; i ; i = nxt[i] )#define cr(x) memset(x , 0 , sizeof x)#define pb push_back#define maxn 5007#define maxm 10007inline int rd(){ char c = getchar(); while (!isdigit(c)) c = getchar() ; int x = c - '0'; while (isdigit(c = getchar())) x = x * 10 + c - '0'; return x;}typedef int arr[maxn];typedef int adj[maxm];int K , n , m , ett;arr vis , head;adj nxt , to , del;char s1[10] , s2[10];vector<int> ans;inline int str2int(char*s){ int t = 0; for (;*s;s ++) { t = t * 10 + (*s) - '0'; } return t;}inline int ids(char*s){ int l = strlen(s + 1); int t = str2int(s + 3); if (s[1] == 'A' && s[2] == 'S') { return t; } else if (s[1] == 'A' && s[2] == 'P') { return t + K; } else if (s[1] == 'B' && s[2] == 'S') { return t + n + K; } else if (s[1] == 'B' && s[2] == 'P') { return t + n + K + K; }}inline void idx(int x){ if (1 <= x && x <= K) { printf("AS%d" , x); } else if (K < x && x <= n + K) { printf("AP%d" , x - K); } else if (n + K < x && x <= n + K + K) { printf("BS%d" , x - n - K); } else if (n + K + K < x && x <= n + m + K + K) { printf("BP%d" , x - n - K - K); }}inline void ins(int u , int v) { to[++ ett] = v , nxt[ett] = head[u] , head[u] = ett; to[++ ett] = u , nxt[ett] = head[v] , head[v] = ett;}bool dfs(int u , int pre_E){ int cnt = 0 , is_leaf = 1; fore (i , u) if (i != (pre_E ^ 1)) { int v = to[i]; is_leaf = 0; if (!dfs(v , i)) { return 0; } cnt += del[i] ^ 1; } if (cnt == 2 && pre_E) { del[pre_E] = del[pre_E ^ 1] = 1; } return is_leaf || cnt == 1 || cnt == 2;}bool dfs_circle(int u){ int cnt = 0; vis [u] = 1; ans.pb (u) ; fore (i , u) if (!del[i]) { int v = to[i]; ++ cnt; if (!vis[v] && !dfs_circle(v)) { return 0; } } return cnt == 2;}void read_and_add(){ scanf("%s%s" , s1 + 1 , s2 + 1); ins(ids(s1) , ids(s2));}void print(){ for (vector<int>::iterator it = ans.begin() ; it != ans.end() ; it ++) { printf(" "); idx(*it); } puts("");}void solve(){ bool flag = 1; K = rd() , n = rd() , m = rd(); rep (i , 1 , n + K - 1) read_and_add(); flag = flag && dfs(1 , 0); rep (i , 1 , m + K - 1) read_and_add(); if (flag) { flag = flag && dfs(n + K + 1 , 0); } rep (i , 1 , K) read_and_add(); if (flag) { flag = flag && dfs_circle(1); } if (flag && ans.size() == m + n + K + K) { printf("YES"); print(); } else { puts("NO"); }}void init(){ ett = 1; ans.clear(); cr(head) , cr(del) , cr(nxt) , cr(vis);}int main(){ #ifndef ONLINE_JUDGE freopen("data.txt" , "r" , stdin); #endif rep (T , 1 , rd()) { init (); solve(); } return 0;}
0 0
- 【bzoj4065】Graphic Madness
- UVALive 6265 Graphic Madness 搜索 Regionals 2012 :: Europe - Central
- BZOJ3958: [WF2011]Mummy Madness
- codeforces822F Madness -- 贪心
- JavaScript Madness: Dynamic Script Loading
- JavaScript Madness: Dynamic Script Loading
- BZOJ 3958: [WF2011]Mummy Madness
- Graphic Design
- Graphic编码
- graphic Shader
- graphic Xfermode
- graphic擦除
- Graphic --- Android
- Graphic Architecture
- 对话#26:A Midsummer Night’s Madness
- Configure/Build System Madness under *nix
- Four Methods to the Madness(1)
- SFX:property caching madness in JIT
- mybatis:java.lang.IllegalArgumentException:Parameter Maps collection does notcontain value for ...
- LeetCode:Palindrome Linked List
- 109 You want to take a complete database backup using RMAN. The backup should consist only the used
- 1、Two Sum
- Reachability实时网络监听/AFNETWorking2.0+ 网络监听
- 【bzoj4065】Graphic Madness
- Oracle语句的再深化--转码
- ASP.NET应用程序与页面生命周期
- 求二进制数中1的个数
- 浅析struts2中的验证
- java批量命名文件名
- JSOI2013 密码
- Android-Tasks and Back Stack
- C++ lambda algorithm