[HDOJ1827]Summer Holiday(强连通分量,缩点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1827
缩点后统计入度和当前强连通分量中最小花费,然后记录入度为0的点的个数和花费和就行了。
- /*
- ━━━━━┒ギリギリ♂ eye!
- ┓┏┓┏┓┃キリキリ♂ mind!
- ┛┗┛┗┛┃\○/
- ┓┏┓┏┓┃ /
- ┛┗┛┗┛┃ノ)
- ┓┏┓┏┓┃
- ┛┗┛┗┛┃
- ┓┏┓┏┓┃
- ┛┗┛┗┛┃
- ┓┏┓┏┓┃
- ┛┗┛┗┛┃
- ┓┏┓┏┓┃
- ┃┃┃┃┃┃
- ┻┻┻┻┻┻
- */
- #include <algorithm>
- #include <iostream>
- #include <iomanip>
- #include <cstring>
- #include <climits>
- #include <complex>
- #include <fstream>
- #include <cassert>
- #include <cstdio>
- #include <bitset>
- #include <vector>
- #include <deque>
- #include <queue>
- #include <stack>
- #include <ctime>
- #include <set>
- #include <map>
- #include <cmath>
- using namespace std;
- #define fr first
- #define sc second
- #define cl clear
- #define BUG puts("here!!!")
- #define W(a) while(a--)
- #define pb(a) push_back(a)
- #define Rint(a) scanf("%d", &a)
- #define Rll(a) scanf("%I64d", &a)
- #define Rs(a) scanf("%s", a)
- #define Cin(a) cin >> a
- #define FRead() freopen("in", "r", stdin)
- #define FWrite() freopen("out", "w", stdout)
- #define Rep(i, len) for(int i = 0; i < (len); i++)
- #define For(i, a, len) for(int i = (a); i < (len); i++)
- #define Cls(a) memset((a), 0, sizeof(a))
- #define Clr(a, x) memset((a), (x), sizeof(a))
- #define Full(a) memset((a), 0x7f7f, sizeof(a))
- #define pi 3.14159265359
- #define RT return
- #define lowbit(x) x & (-x)
- #define onenum(x) __builtin_popcount(x)
- typedef long long LL;
- typedef long double LD;
- typedef unsigned long long ULL;
- typedef pair<int, int> pii;
- typedef pair<string, int> psi;
- typedef map<string, int> msi;
- typedef vector<int> vi;
- typedef vector<LL> vl;
- typedef vector<vl> vvl;
- typedef vector<bool> vb;
- const int maxn = ;
- const int maxm = ;
- const int inf = 0x7f7f7f;
- int n, m;
- vector<int> G[maxn];
- int vis[maxn][maxn];
- int w[maxn];
- int dfn[maxn], low[maxn], idx;
- int st[maxn], top;
- int belong[maxn], bcnt;
- int in[maxn], out[maxn];
- int cost[maxn];
- void tarjan(int u) {
- int v = u;
- dfn[u] = low[u] = ++idx;
- vis[][u] = ; st[++top] = u;
- Rep(i, G[u].size()) {
- v = G[u][i];
- if(!dfn[v]) {
- tarjan(v);
- low[u] = min(low[u], low[v]);
- }
- else if(vis[][v]) low[u] = min(low[u], dfn[v]);
- }
- if(low[u] == dfn[u]) {
- bcnt++;
- do {
- v = st[top--];
- vis[][v] = ;
- belong[v] = bcnt;
- } while(u != v);
- }
- }
- int main() {
- // FRead();
- int u, v;
- while(~Rint(n) && ~Rint(m)) {
- Cls(dfn); Cls(low); Cls(w);
- Cls(st); Cls(vis); top = ;
- Cls(belong); bcnt = ; idx = ;
- Cls(in); Cls(out);
- For(i, , n+) {
- Rint(w[i]);
- G[i].cl();
- cost[i] = inf;
- }
- Rep(i, m) {
- Rint(u); Rint(v);
- G[u].pb(v);
- }
- For(i, , n+) if(!dfn[i]) tarjan(i);
- Cls(vis);
- For(u, , n+) {
- cost[belong[u]] = min(cost[belong[u]], w[u]);
- Rep(i, G[u].size()) {
- int v = G[u][i];
- if(belong[u] == belong[v]) continue;
- in[belong[v]]++;
- out[belong[u]]++;
- }
- }
- int ret1 = , ret2 = ;
- For(i, , bcnt+) {
- if(in[i] == ) {
- ret1++;
- ret2 += cost[i];
- }
- }
- printf("%d %d\n", ret1, ret2);
- }
- RT ;
- }
[HDOJ1827]Summer Holiday(强连通分量,缩点)的更多相关文章
- POJ1236Network of Schools[强连通分量|缩点]
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16571 Accepted: 65 ...
- POJ1236Network of Schools(强连通分量 + 缩点)
题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...
- HD2767Proving Equivalences(有向图强连通分量+缩点)
题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...
- UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)
题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...
- ZOJ3795 Grouping(强连通分量+缩点+记忆化搜索)
题目给一张有向图,要把点分组,问最少要几个组使得同组内的任意两点不连通. 首先考虑找出强连通分量缩点后形成DAG,强连通分量内的点肯定各自一组,两个强连通分量的拓扑序能确定的也得各自一组. 能在同一组 ...
- POJ2553 The Bottom of a Graph(强连通分量+缩点)
题目是问,一个有向图有多少个点v满足∀w∈V:(v→w)⇒(w→v). 把图的强连通分量缩点,那么答案显然就是所有出度为0的点. 用Tarjan找强连通分量: #include<cstdio&g ...
- uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...
- poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: ...
- tarjan算法(强连通分量 + 强连通分量缩点 + 桥(割边) + 割点 + LCA)
这篇文章是从网络上总结各方经验 以及 自己找的一些例题的算法模板,主要是用于自己的日后的模板总结以后防失忆常看看的, 写的也是自己能看懂即可. tarjan算法的功能很强大, 可以用来求解强连通分量, ...
- LA 4287 等价性证明(强连通分量缩点)
https://vjudge.net/problem/UVALive-4287 题意: 给出n个结点m条边的有向图,要求加尽量少的边,使得新图强连通. 思路:强连通分量缩点,然后统计缩点后的图的每个结 ...
随机推荐
- String对象中常用的方法
String对象中常用的方法 1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码.strObj.charCodeAt(index)说明:index将被处理字符的从零开始 ...
- margin-top相对谁的问题
根据规范,一个盒子如果没有上补白(padding-top)和上边框(border-top),那么这个盒子的上边距会和其内部文档流中的第一个子元素的上边距重叠.意思便是:如果你只想margin相对于父标 ...
- .net 自然排序方式
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- mysql中实现oracle中的rowid功能
mysql中没有函数实现,只能自己手动添加变量递增 := 就是赋值,只看红色字体就行 select @rownum:=@rownum+1,img.img_path,sku.sku_name from ...
- .Xresources 配置文件
安装rxvt-unicode-256color,如果不是这个版本的话VIM配色会显示不正常. ~/.Xresources配置文件如下 !urxvt color scheme: URxvt*backgr ...
- [转载+原创]Emgu CV on C# (六) —— Emgu CV on Canny边缘检测
Canny边缘检测也是一种边缘检测方法,本文介绍了Canny边缘检测的函数及其使用方法,并利用emgucv方法将轮廓检测解算的结果与原文进行比较. 图像的边缘检测的原理是检测出图像中所有灰度值变化较大 ...
- 1021 玛丽卡 - Wikioi
题目描述 Description麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们知 ...
- 1069: [SCOI2007]最大土地面积 - BZOJ
Description 在某块平面土地上有N个点,你可以选择其中的任意四个点,将这片土地围起来,当然,你希望这四个点围成的多边形面积最大.Input 第1行一个正整数N,接下来N行,每行2个数x,y, ...
- IntelliJ IDEA 比较当前版本文件与历史文件
前言: 写代码修改后怎样比较与历史文件的区别呢?idea提供了2种比较方式(目前笔者所了解到的) 一.SVN的版本比较 二.当前文件与历史版本比较
- shell 循环使用
问题描述: shell中for循环while循环的使用 问题解决: (1)for循环 (1.1)数 ...