<题目链接> 题目大意: 给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同. 解题分析:本题想到拓扑排序就好做了.就是枚举每个字符串,每个字符串和它前一个字符串寻找第一个不同的字符,然后前一个串的该字符向当前串的字符连一条有向边(注意判断后一个串是前一个串的子串的情况).然后就是跑一遍拓扑排序,如果不冲突的话,就可构造出答案. #include <bits/stdc++.h> using n…
题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来,将每个名字的第x个字母从上到下连接建图.然后求拓扑排序. 之所以要拓扑排序,因为要判断在x-1里面有a-->b  在x中有b-->a,这样就形成了一个环.这样一来,就不能够构造字母表了. [经验教训]:在递归建图的函数中开了两个数组,用来记录字母第一次出现和最后一次出现的位置..结果就RE在12上…
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the…
题目链接:http://codeforces.com/problemset/problem/510/C Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order.…
省委代码: #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #define maxn 100 #define maxm 10000 using namespace std; struct edge{ int next,to; }e[maxm]; int point[ma…
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the…
C. Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: t…
You are given a program you want to execute as a set of tasks organized in a dependency graph. The dependency graph is a directed acyclic graph: each task can depend on results of one or several other tasks, and there are no directed circular depende…
大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环 解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就YES,都不行就NO 复杂度O(N*M)看起来不超时 但是实现了以后发现即使优化到不清空vis数组(时间戳标记),也仍然超时. 因为O(N*M)已经很接近时间复杂度上界,常数稍大就GG. 不过可以脑补一下取巧算法:在不超时的前提下,随机取K个边进行检验~~~.不过数据多了就非常容易GG.理论上还是可行的. 正解:从枚举边变为枚举点,删掉到达一个点的某条边可以认为是该点入…
D. Substring time limit: per test3 seconds memory limit: per test256 megabytes inputstandard: input outputstandard: output You are given a graph with nnn nodes and mmm directed edges. One lowercase letter is assigned to each node. We define a path's…
C. Mail Stamps     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», they stamp it with «A B», or «B A». Unfortunately, often it is impossible to send a letter direc…
题目链接  National Property 给定n个单词,字符集为m 现在我们可以把其中某些字母变成大写的.大写字母字典序大于小写字母. 问是否存在一种方案使得给定的n个单词字典序不下降. 首先判断是否存在相邻的两个单词,后一个是前一个的前缀.(两者不相等) 如果出现这种情况则直接输出无解. 建立起点s和终点t.每次扫描相邻的两个单词,找到他们第一个不相等的字母的位置. 和s相连的字母必须改成大写,和t相连的字母必须不能改成大写. 设前面那个单词的这个位置的字母为a, 后面的为b 如果a >…
题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #include <cstdio> #include <iostream> #include <cstring> #include <string> #include <map> #include <algorithm> #include &…
原题:http://codeforces.com/problemset/problem/510/C C. Fox And Names time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Sys…
[题目链接]:http://codeforces.com/contest/510/problem/C [题意] 给你n个字符串; 问你要怎么修改字典序; (即原本是a,b,c..z现在你可以修改每个字母在字典序中的位置了); 才能使得这n个字符串是字典序升序的; [题解] 对于第i个字符串; 让他和第i+1..n个字符串比较->j; 找到第一个不同的位置pos; 然后 必然是要 s[i][pos]< s[j][pos]的: 拓扑排序! 即s[i][pos]建一条边指向s[j][pos] 然后做…
C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: "Fox"). She heard a rumor: the authors list on the paper is always sorted in…
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…
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…
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…
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…
题意:给你一颗树,问这颗树是否存在一个根,使得对于任意两点,如果它们到根的距离相同,那么它们的度必须相等. 思路1:树的重心乱搞 根据样例发现,树的重心可能是答案,所以我们可以先判断一下树的重心可不可以.如果不行,剩下的只可能是度为1点当根了.当然,我们不能枚举所有度为1的点,不然一个菊花图就超时了,我的做法是对于以重心为根的树搜索一遍,对于每个深度的度数为1的点只记录一个,然后枚举这些点,如果有就是有,否则没有.这样最坏的复杂度应该能到O(n * sqrt(n)),但是肯定跑不满.至于为什么这…
E - E CodeForces - 1100E 一个n个节点的有向图,节点标号从1到n,存在m条单向边.每条单向边有一个权值,代表翻转其方向所需的代价.求使图变成无环图,其中翻转的最大边权值最小的方案,以及该方案翻转的最大的边权. Input 单组输入,第一行包含两个整数n和m(2≤n≤100 000,1≤m≤100 000) 接下来m行,每行3个整数,u_i ,v_i ,w_i (1<= u_i , v_i <= n, 1<= w_i <= 10^9),表示u到v有一条权值为w…
https://codeforces.com/contest/1217 D:给定一个有向图,给图染色,使图中的环不只由一种颜色构成,输出每一条边的颜色 不成环的边全部用1染色 ps:最后输出需要注意,一个环上的序号必然是非全递增的,若有环且有一条边u->v,u的序号<v则输出1否则输出2(反过来也可以) 可以用dfs染色或者用拓扑排序做 顺便复习一下拓扑排序: 拓扑排序是将有向无环图的所有顶点排成一个线性序列,使得图中任意两个顶点u,v若存在u->v,那么序列中u一定在v前面. 了解一个…
题目链接:https://codeforces.com/contest/1368/problem/E 题意 给出一个 $n$ 点 $m$ 边的有向图,每条边由编号较小的点通向编号较大的点,每个点的出度不大于 $2$,删掉一些点,使得图中不存在长度大于等于 $2$ 的路径.(最多删掉 $\frac{4}{7}n$ 个点) 题解 删除所有拓扑排序中深度为 $3$ 的倍数的顶点,由于每次删掉了一层,所以把所有点深度置为 $1$,只删除深度为 $3$ 的顶点即可. 证明 删除点占比最大的情况是该有向图为…
先做拓扑排序,再bfs处理 #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<, INF = , , ; } ;i<=n; i++) {         ) {             st.push(i);         }     }     ;         ; i = edge[i].next){             ;…
题目链接: 题目 A. Misha and Forest time limit per test 1 second memory limit per test 256 megabytes 问题描述 Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n ver…