Almost Acyclic Graph Codeforces - 915D】的更多相关文章

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…
大意: 给定无向图, 求是否能删除一条边后使图无环 直接枚举边判环复杂度过大, 实际上删除一条边可以看做将该边从一个顶点上拿开, 直接枚举顶点即可 复杂度$O(n(n+m))$ #include <iostream> #include <algorithm> #include <string.h> #include <math.h> #include <cstdio> #include <queue> #define REP(i,a,…
以前做过的题都不会了.... 此题做法:优化的暴力 有一个显然的暴力:枚举每一条边试着删掉 注意到题目要求使得图无环,那么找出图上任意一个环,都应当要在其某一处断开(当然没有环是YES) 因此找出图中任意一个简单环(点不重复),枚举断开其上每一条边即可(共最多n条边) 复杂度O(n*(n+m)) 注意:不能用拓扑排序找出不能被排序的点来找环,因为拓扑排序后入度不为0的不一定是环上的点(比如可能是某个点,没有出边,仅有一条入边,是某个环上的点引出的)(曾经错了) #include<cstdio>…
[题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一个简单环,则欲删除的边一定经过该环.尝试环上的每一条边(至多n条边)后再次拓扑排序判断全图是否有环. 拓扑排序后定位到简单环:剩余图是环+环内DAG,DFS过程中将走入死路的点标-1,访问过标1,找到访问过的点就是简单环.换起始点直到找到环为止. 复杂度O(nm). #include<cstdio>…
Given a Weighted Directed Acyclic Graph and a source vertex in the graph, find the shortest paths from given source to all other vertices. For a general weighted graph, we can calculate single source shortest distances in O(VE) time using Bellman–For…
Bubble Sort Graph CodeForces - 340D 题意: 给出一个n个数的数列,建一个只有n个结点没有边的无向图,对数列进行冒泡排序,每交换一对位置在(i,j)的数在点i和点j间连一条边.排序完后,求得到图的最大独立集. 解释: 最初想到的是图的最大独立集,认为不能解,于是就没辙了... 然而应当仔细分析题目(不容易想到):题意很容易知道是在每一对逆序对间连一条边.也就是说,对于a[i],向a[i]右侧比其小的和a[i]左侧比其大的都要连一条边.也就是说,只要选了结点i,那…
D - Beautiful Graph CodeForces - 1093D You are given an undirected unweighted graph consisting of nn vertices and mm edges. You have to write a number on each vertex of the graph. Each number should be 11, 22or 33. The graph becomes beautiful if for…
D. Almost Acyclic Graph 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 be traversed in…
Description You are given a directed graph consisting of \(n\) vertices and \(m\) edges (each edge is directed, so it can be traversed in only one direction). You are allowed to remove at most one edge from it. Can you make this graph acyclic by remo…
大意:给出一个有向图,问能否在只去掉一条边的情况下破掉所有的环 解析:最直接的是枚举每个边,将其禁用,然后在图中找环,如果可以就YES,都不行就NO 复杂度O(N*M)看起来不超时 但是实现了以后发现即使优化到不清空vis数组(时间戳标记),也仍然超时. 因为O(N*M)已经很接近时间复杂度上界,常数稍大就GG. 不过可以脑补一下取巧算法:在不超时的前提下,随机取K个边进行检验~~~.不过数据多了就非常容易GG.理论上还是可行的. 正解:从枚举边变为枚举点,删掉到达一个点的某条边可以认为是该点入…
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找到任意一个环. 然后枚举删掉其中的某一条边即可. (因为肯定要删掉这个环的,那么方法自然就是删掉其中的某一条边 (其它环,如果都包括这条边,那么就可以,否则,某个环不包括那也没办法,自然就无解了. 这样枚举的边的数目最多是500(环最多N条边) 然后复杂度就是500*10万了. 足够过了 [代码] /* 1.Shoud it use long long ? 2.Have you ever test several sample…
思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上差分 ] | Codeforces Round #429(Div 1) 题意: 选择一个边集合,满足某些点度数的奇偶性 分析: 将d = 1的点连成一颗树,不在树上的点都不连边. 可以发现,当某个节点u的所有子节点si均可操控 (u, si) 来满足自身要求 即整棵树上至多只有1个点不满足自身要求,…
Mr. Kitayuta has just bought an undirected graph with n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi. Mr. Kitayuta wants you to process the following q…
http://codeforces.com/problemset/problem/916/C 好尬的题啊... #include<cstdio> #include<algorithm> #include<cstring> #include<vector> using namespace std; #define fi first #define se second #define mp make_pair #define pb push_back typed…
这道题我第一次的想法是直接判环的数量,然而事实证明实在是太naive了. 随便画个图都可以卡掉我的解法.(不知道在想什么) 这道题的正解是拓扑排序. 朴素的想法是对所有边都跑一次拓扑,但这样$O(m(n+m))$会炸,于是可以有下面的优化. 我们找到所有入度不为零的点,然后把他们每一个都删掉一条边跑一遍拓扑排序. 那么这样就可以优化到$O(n(n+m))$了,稳得一批. AC代码如下: 1935ms 1356kb #include<bits/stdc++.h> using namespace…
Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wanted to give her an affectionate gift but couldn't think of anything inventive. Hence, he will be giving her a graph. How original, Bob! Alice will sur…
题目链接:http://codeforces.com/contest/915/problem/D 题目大意: 给出一个\(n\)个结点\(m\)条边的有向图(无自环.无重边,2 ≤ n ≤ 500, 1 ≤ m ≤ min(n(n - 1), 100000) ),问能否通过删除其中的某一条边,使得该图无环. 知识点: 拓扑排序.DFS 解题思路一: 由于结点数不多,所以可以枚举每个入度不为\(0\)的点,删去通向它的一条边(即使其入度减一),再跑拓扑排序判断有没有环. AC代码一: #inclu…
条件: 1.每个顶点出现且只出现一次. 2.若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面. 有向无环图(DAG)才有拓扑排序,非DAG图没有拓扑排序一说. 一般用有向边指示顺序关系,运用于顺序关系. 例如,下面这个图: 显然是一个DAG图,1→4表示4的入度+1,4是1的邻接点, 代码表示:前者deg[4]++;后者用vector[1].push(4) 如何写出拓扑排序代码? 1.首先将边与边的关系确定,建立好入度表和邻接表. 2.从入度为0的点开始删除…
大意:给定二分图, 求将边染色, 使得任意邻接边不同色且使用颜色种类数最少 最少颜色数即为最大度数, 要输出方案的话, 对于每一条边(u,v), 求出u,v能使用的最小的颜色$t0$,$t1$ 若t0=t1, 直接染就行不会有冲突, 否则就染为t0, 这样的话可能产生冲突, 就将冲突的边换成t1, 依次递归下去 由于二分图的性质, 最终一定可以找到一条边不冲突 #include <iostream> #include <algorithm> #include <cstdio&…
链接 大意:给定$m$个数, 若$x\&y=0$, 则在$x$与$y$之间连一条无向边. 求无向图的连通块个数 暴力连边显然超时的, 可以通过辅助结点优化连边, 复杂度$O(n2^n)$ #include <iostream> #include <algorithm> #include <cstdio> #include <queue> #define REP(i,a,n) for(int i=a;i<=n;++i) #define pb pu…
链接 大意: 给定无向连通图, 每个点有权值$d_i$($-1\leq d_i \leq 1$), 求选择一个边的集合, 使得删除边集外的所有边后, $d_i$不为-1的点的度数模2等于权值 首先要注意到该题只需要考虑dfs树即可, 因为反向边一定不会产生贡献 存在权值为-1的点, 则直接以权值为-1的点为根dfs 若无权值为-1的点, 则答案不一定存在, 任选一个点为根dfs即可 #include <iostream> #include <algorithm> #include…
题意:给出n(0≤n≤22)和m,和m个数ai,1 ≤ m ≤ 2n ,0≤ai<2n ,把ai & aj == 0 的连边,求最后有几个连通块 解析:一个一个去找肯定爆,那么就要转换一下思维,想一下什么样的数才能按位与ai为0 那么肯定是ai ^ ((1<<n)-1)的子集,所以去找它的所有子集即可 例1010  变成0101  子集有 0101  0100  0001 然后只有x是给出的那m个数种的时候 才能 ^ ,其他情况消1取子集 #include <bits/st…
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Misha and Vasya participated in a Codeforces contest. Unfortunately, each of them solved only one problem, though successfully sub…
You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multiple edges between any pair of vertices. Graph can be disconnected. You should assign labels to all vertices in such a way that: Labels form a valid pe…
B. Kay and Snowflake 题目连接: http://www.codeforces.com/contest/685/problem/B Description After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes. Once upon a time,…
E. Famil Door and Roads 题目连接: http://www.codeforces.com/contest/629/problem/E Description Famil Door's City map looks like a tree (undirected connected acyclic graph) so other people call it Treeland. There are n intersections in the city connected b…
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Misha and Vasya participated in a Codeforces contest. Unfortunately, each of them solved only one problem, though successfully sub…
A. Eleven time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Eleven wants to choose a new name for herself. As a bunch of geeks, her friends suggested an algorithm to choose a name for her. El…
题目简述:给定字符串$s[1 \dots n](n \leq 2 \times 10^5)$,以及$Q \leq 2 \times 10^5$个询问,每个询问有两个参数$1 \leq l \leq r \leq n$,求 $$ \sum_{i=l}^r \operatorname{lcp}(s[l \dots r], s[i \dots r]), $$ 其中$\operatorname{lcp}(s, t)$表示字符串$s$和$t$的最长公共前缀(Longest Common Prefix)的长…
E. Danil and a Part-time Job 题目链接:http://codeforces.com/contest/877/problem/E time limit per test2 seconds memory limit per test256 megabytes Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he i…