Making Genome in Berland time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Berland scientists face a very important task - given the parts of short DNA fragments, restore the dinosaur DNA! Th…
代码不知道上了多少补丁..终于过了 用类似拓扑排序的办法收缩整棵树得到x,然后找到x直连的最远的和最近的点 只有这三个点可能是根,依次判一下即可 另外题解的第一种方法时找直径,然后判两端点+重心+所有直连重心的叶子节点,感觉这样子复杂度爆炸啊..(如果是遍历所有叶子节点的话) #include<bits/stdc++.h> using namespace std; #define maxn 200005 ]; int head[maxn],tot; void init(){ memset(he…
题目链接:http://codeforces.com/contest/883/problem/B There are n military men in the Berland army. Some of them have given orders to other military men by now. Given m pairs (xi, yi), meaning that the military man xi gave the i-th order to another milita…
Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input Output The first line of output should contain "Yes", if it's possible to do a correct evaluation for all the dishes, or "No" otherwis…
[CF883B]Berland Army 题意:给出n个点,m条有向边,有的点的点权已知,其余的未知,点权都在1-k中.先希望你确定出所有点的点权,满足: 对于所有边a->b,a的点权>b的点权对于i=1..k,至少有一个点的点权为i n,m,k<=100000 题解:像菜肴制作一样奇怪的拓扑排序题,直接上方法吧,不会证. 先正反跑两边拓扑排序,得出每个点点权的下界Li和上界Ri. 将所有点按上界从小到大排序,然后枚举权值i.将所有上界为i的点都扔到堆中,再从堆里取出下界最大的那个点,将…
先做拓扑排序,再bfs处理 #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<, INF = , , ; } ;i<=n; i++) {         ) {             st.push(i);         }     }     ;         ; i = edge[i].next){             ;…
[题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一个简单环,则欲删除的边一定经过该环.尝试环上的每一条边(至多n条边)后再次拓扑排序判断全图是否有环. 拓扑排序后定位到简单环:剩余图是环+环内DAG,DFS过程中将走入死路的点标-1,访问过标1,找到访问过的点就是简单环.换起始点直到找到环为止. 复杂度O(nm). #include<cstdio>…
While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the…
题目链接: http://codeforces.com/problemset/problem/645/D 题意: 给定n个机器人的m个能力大小关系,问你至少要前几个大小关系就可以得到所有机器人的能力顺序. 分析: 拓扑+二分. 注意最终的顺序不能缺点,先把度为0的点入队,如果度为0的点的个数大于1,则说明至少有两个点的能力大小不确定,无法继续. 代码: #include<bits/stdc++.h> using namespace std; const int maxn = 100005; v…
题意:给出n串名字,表示字典序从小到大,求符合这样的字符串排列的字典序 先挨个地遍历字符串,遇到不相同的时候,加边,记录相应的入度 然后就是bfs的过程,如果某一点没有被访问过,且入度为0,则把它加入队列中,并将它指向的节点的入度减去1 另外如果len[i]<len[i-1],说明第i个字符串是i-1个字符串的前缀,但是它还排在后面,这样肯定不存在符合的字典序,直接输出“Impossible” #include<iostream> #include<cstdio> #incl…
题目链接: D. Robot Rapping Results Report time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alt…
懒得复制,戳我戳我 Solution: 我感觉我也说不太好,看Awson的题解吧. 说一点之前打错的地方: 连边存的是hash后的数组下标 if(ans[ num( C[a.hash()] , C[b.hash()] ) ]==1){ if(f==0)printf("Alice\n");else printf("Bob\n"); continue; } if(ans[ num( C[a.hash()] , C[b.hash()] ) ]==-1){ if(f==0)…
Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3178    Accepted Submission(s): 988 Problem Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, a…
Usoperanto Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/contest_show.php?cid=6866#problem/J Description Usoperanto is an artificial spoken language designed and regulated by Usoperanto Academy. The academy is now in study to es…
Ponds Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1001&cid=621 Description Betty owns a lot of ponds, some of them are connected with other ponds by pipes, and there will not be more than one…
第一题:太鼓达人:BZOJ3033 题意:给出k,求一个最长的M位01串,使其从每一个位置向后走k个得到 的M个k位01串互不相同(最后一个和第一个相邻,即是一个环).输出 字典序最小的答案. 2 ≤ k ≤ 11. 结论+爆搜: 第二问我们将每个k二进制数看成一个点,在它后面加上0/1就能得 到两个新数,我们从这个数向两个新数连边,于是这就变成了求一个欧 拉回路的问题.显然此题是存在欧拉回路的第一问答案为2的k次方 ,第二问暴力求欧拉回路即可. 发现sjh的注释很详细,所以贿赂搞来了一个满满注…
目录 题目链接 题解 代码 题目链接 AGC027 C - ABland Yard 题解 发现有解的充要条件是有一个形为AABBAABBAABB的环 此时每一个点至少与两个不同颜色的点相连 对于初始不合法的点直接删掉,判断删掉后与其相连的点是否变为不合法 类似拓扑排序 代码 #include<bits/stdc++.h> #define gc getchar() #define pc putchar inline int read() { int x = 0,f = 1; char c = g…
原文地址:http://www.cnblogs.com/GXZlegend/p/6832118.html 题目描述 如图所示为某生态系统的食物网示意图,据图回答第1小题 现在给你n个物种和m条能量流动关系,求其中的食物链条数. 物种的名称为从1到n编号 M条能量流动关系形如 a1 b1 a2 b2 a3 b3 ...... am-1 bm-1 am bm 其中ai bi表示能量从物种ai流向物种bi,注意单独的一种孤立生物不算一条食物链 输入 第一行两个整数n和m,接下来m行每行两个整数ai b…
题目传送门 题意:给出一条链,比如x连到y,x一定要在y的左边,且代价是这条链经过的点的权值和,问如何排序使得代价最小 分析:类似拓扑排序,先把入度为0的点入队,把指向该点的所有点按照权值排序,保证这样是代价是最小的,然后把这一块看成一个点继续入队.看图更简单: /************************************************ * Author :Running_Time * Created Time :2015/10/3 星期六 13:02:41 * File…
题目链接: http://172.16.0.132/senior/#main/show/5905 题目: oi_juruo热爱一款名叫黑暗之魂的游戏.在这个游戏中玩家要操纵一名有 点生命值的无火的余灰在一张地图中探险.地图中有$n$个篝火(也就是存档点).在篝火处休息可以将生命值恢复满.每个篝火都会向其他篝火的其中之一连有一条通道(显然,通道是双向的),这些篝火之间都相互可达.也就是说,这是一张$n$个点,$n$条边的无向连通图.每条通道里都有一些怪物,经过oi_juruo的分析,他得到了每条边…
1804: 有向无环图 Submit Page   Summary   Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 716     Solved: 298 Description Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 v 开始.点 v 结束的路径). 为了方便,点用 1,2,…,n 编号. 设 count(x,y) 表示点 x 到点 y 不同的路径数量(规定 count(x,x)=0…
大意: n个点, 点$i$的等级为$r_i$, 只给出部分点的$r$值, $r_i$的范围为[1,k], 且[1,k]都至少有一个. 给定m条有向边, (x,y)表示$r[x]>r[y]$, 求一个合法的$r$序列. 我们记条件(1): [1,k]都至少有一个. 条件(2): 所有有向边(x,y), 满足$r[x]>r[y]$. 正反各拓扑一次, 这样可以求出满足条件(2)时每个点的可用等级范围[L,R], 我们考虑在不违背条件(2)的情况下, 使(1)成立. 所有点按$L$排序后, 将$1,…
B. Making Genome in Berland 题目连接: http://www.codeforces.com/contest/638/problem/B Description Berland scientists face a very important task - given the parts of short DNA fragments, restore the dinosaur DNA! The genome of a berland dinosaur has notin…
C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/29/C Description One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city «A» to city «B…
B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a following problem about putting 1 × 2 tiles into an n × m grid: "There is a grid with some cells that are empty and some cells that are occupied. You s…
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > < 跑拓扑排序,根据拓扑排序的结果从小到大填数字就好了,需要注意的细节写在代码注释里了 代码: #include<bits/stdc++.h> using namespace std; ; int f[M],n,m; set<int>st[M]; vector<int&…
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[],使得两个数组中最大的数尽量小 题解 按照偏序表,构造出从小到大的拓扑图 如何解决相等的数的偏序关系? 用并查集缩点后再进行拓扑排序 如何解决最大的数最小? 只需要使得同一层的数相同就行,可以一批处理栈中的元素,对于一批栈中的元素产生的新点,先放进一个容器里,然后等到这批栈清空了,再把这个容器中的点…
E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimize a tree. He can perform the following operation multiple times: choose a vertex v, and two disjoint (except for v) paths of equal length a0 = v, a1,…
题目链接:http://codeforces.com/problemset/problem/894/E 题目大意: $n$个点$m$条边的有向图,每条边有一个权值,可以重复走. 第$i$次走过某条边权为$w$的边后这条边的边权变成$w-i$,但不能小于等于$0$. 给定起点,询问任意走最多能获得多少的边权 题解: 显然一个强联通分量的边可以全部走到$0$为止. 考虑强连通分量中一条边权为w的边对答案的贡献,$s=w+w-1+w-1-2+w-1-2-3\ldots$ 设这个式子有$t+1$项,显然…
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can…