RunID

User

Problem

Result

Memory

Time

Language

Length

Submit Time

2482977

zhyfzy

J

Accepted

0 KB

138 ms

C++ 4.8.2

2322 B

2014-07-24 15:18:54

【题目大意】

一个有向图,每对一个结点操作,就可以触发连锁反应,使得该结点及它直接或间接指向的点均获得标记,问至少需要操作多少个结点使得所有结点获得标记

【题解】

缩点+DFS

首先能想到入度为0的点一定需要操作,但是操作完所有入度为0的点不一定使所有结点获得标记,比如存在环的情况,因此,我们需要先缩点,缩点使用Tarjan算法,详见代码,缩点之后直接统计入度为0的点有多少个即可。

代码采用链式向前星的存储结构

【代码】

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<stack>
#define P 20000
#define E 200000
using namespace std;
int i,j,k,n,T,ans,K,m,x,y,indexs,nn,mm;
int ru[P],head[P],head2[P],dfn[P],low[P],instack[P],belong[P];
bool b[E];
stack <int> tar; struct node
{
int from;
int to;
int next;
}map[E],map2[E]; void addedge(int num,int x,int y)
{
map[num].from=x;
map[num].to=y;
map[num].next=head[x];
head[x]=num;
} void addedge2(int num,int x,int y)
{
//printf("NewEdge %d %d\n",x,y); ru[y]++;
map2[num].from=x;
map2[num].to=y;
map2[num].next=head2[x];
head2[x]=num;
} void dfs(int p)
{
for (int i=head2[p];i!=-1;i=map2[i].next)
{
if (!b[map2[i].to])
{
b[map2[i].to]=true;
dfs(map2[i].to);
}
}
} void tarjan(int k)
{
int p;
tar.push(k);
instack[k]=1;
dfn[k]=low[k]=++indexs;
for(int j=head[k];j!=-1;j=map[j].next)
{
p=map[j].to;
if (instack[p])
{
low[k]=min(low[k],dfn[p]);
}
else
if(dfn[p]==-1)
{
tarjan(p);
low[k]=min(low[k],low[p]);
}
}
if(low[k]==dfn[k])
{
nn++;
do
{
j=tar.top();
tar.pop();
instack[j]=0;
belong[j]=nn;
}while(j!=k);
}
} void build_new_map()
{
for(int i=1;i<=m;i++)
{
if(belong[map[i].from]==belong[map[i].to])
continue;
addedge2(++mm,belong[map[i].from],belong[map[i].to]);
}
}
void build_map()
{
scanf("%d%d",&n,&m);
for (i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
addedge(i,x,y);
} memset(dfn,-1,sizeof(dfn));
memset(low,-1,sizeof(low));
memset(instack,0,sizeof(instack));
indexs=0;nn=0; for (i=1;i<=n;i++) belong[i]=i; for (i=1;i<=n;i++)
{
if (dfn[i]==-1)
tarjan(i);
} build_new_map();
} int main()
{
scanf("%d",&T);
while (++K<=T)
{
memset(map,-1,sizeof(map));
memset(head,-1,sizeof(head));
memset(map2,-1,sizeof(map2));
memset(head2,-1,sizeof(head2));
memset(b,0,sizeof(b));
memset(ru,0,sizeof(ru));
ans=0;mm=0;nn=0; build_map(); for (i=1;i<=nn;i++)
{
if (!ru[i])
{
ans++;
b[i]=true;
dfs(i);
}
} for (i=1;i<=nn;i++)
{
if (!b[i])
{
ans++;
b[i]=true;
dfs(i);
}
}
printf("Case %d: %d\n",K,ans);
}
}

  

UVA 11770 Lighting Away的更多相关文章

  1. 【线性结构上的动态规划】UVa 11400 - Lighting System Design

    Problem F Lighting System Design Input: Standard Input Output: Standard Output You are given the tas ...

  2. UVa 11400 - Lighting System Design(线性DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. UVa 11400 Lighting System Design(DP 照明设计)

    意甲冠军  地方照明系统设计  总共需要n不同类型的灯泡  然后进入 每个灯电压v  相应电压电源的价格k  每一个灯泡的价格c   须要这样的灯泡的数量l   电压低的灯泡能够用电压高的灯泡替换   ...

  4. UVa 11400 Lighting System Design

    题意: 一共有n种灯泡,不同种类的灯泡必须用不同种电源,但同一种灯泡可以用同一种电源.每种灯泡有四个参数: 电压值V.电源费用K.每个灯泡的费用C.所需该种灯泡的数量L 为了省钱,可以用电压高的灯泡来 ...

  5. UVa 11400 Lighting System Design【DP】

    题意:给出n种灯泡,分别给出它们的电压v,电源费用k,每个灯泡的费用c,和所需灯泡的数量l,问最优方案的费用 看的紫书= = 首先是dp[i]为灯泡1到i的最小费用, dp[i]=min(dp[i], ...

  6. UVA - 11400 Lighting System Design (区间DP)

    这个问题有两个点需要注意: 1. 对于一种灯泡,要么全换,要么全不换. 证明: 设一种灯泡单价为p1,电池价格为k1,共需要L个,若把L1个灯泡换成单价为p2,电池为k2的灯泡,产生的总花费为p1*L ...

  7. uva 11400 - Lighting System Design(动态规划 最长上升子序列问题变型)

    本题难处好像是在于 能够把一些灯泡换成电压更高的灯泡以节省电源的钱 .所以也才有了对最优方案的探求 好的处理方法是依照电压从小到大排序.仅仅能让前面的换成后面的.也就满足了把一些灯泡换成电压更高的灯泡 ...

  8. UVA 11400 Lighting System Design 照明系统设计

    首先是一个贪心,一种灯泡要么全都换,要么全都不换. 先排序,定义状态d[i]为前面i种灯泡的最小花费,状态转移就是从d[j],j<i,加上 i前面的j+1到i-1种灯泡换成i的花费. 下标排序玩 ...

  9. UVA 11400_ Lighting System Design

    题意: 给定一系列灯泡的额定功率,电源价钱,一个灯泡的价格以及系统所需该种灯泡的数量.已知流过灯泡的电流相等,所以为省钱可以将电压小的灯泡换成电压大的灯泡,但是不能换成电压更小的灯泡,问最少要花多少钱 ...

随机推荐

  1. Starting and Stopping Oracle Fusion Middleware

    指定用户名密码启动管理服务器 You can start and stop Oracle WebLogic Server Administration Servers using the WLST c ...

  2. 深入浅出理解QTimeLine类

    网上找了下QTimeLIne类的介绍,要么就是代码一贴自己看去,要么就是说不到重点,正巧自己项目遇到这个类,在这里写一下,给需要的同学看下. 因为我最近需要有动画方面配合时间间隔触发QGraphics ...

  3. 在PyQt4中使用matplotlib

    matplotlib作为Python中著名的数据可视化工具,其官网也提供了在PyQt4中使用的源码,这里举一个应用实例,以备不时之需. 1) 利用Qt Designer创建GUI界面 Demo的GUI ...

  4. struts2.xml的配置与技巧

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...

  5. 转:PHP乱码问题,UTF-8(乱码)

    原文来自于:http://www.cnblogs.com/rickyNote/archive/2012/04/09/2438704.html 一.HTML页面转UTF-8编码问题 1.在head后,t ...

  6. 转:使用Android API最佳实践

    原文来自于:http://blog.jobbole.com/65170/ 写在前面 现在,Android应用程序中集成第三方API已十分流行.应用程序都有自己的网络操作和缓存处理机制,但是大部分比较脆 ...

  7. jquery 获取选中的文字.当前光标所在的位置等jquery-fieldselection 插件

    写词典在线编辑器用到的一个功能 能获取选中的文字.当前的光标的位置 等位置,而且支持多个文本框一起操作 非常方便 git地址:https://github.com/localhost/jquery-f ...

  8. JavaScript Structure

    Element, Data, Event, Logic Data:----------- 1. method 1 var obj = { name : "thinkpad", ag ...

  9. Cracking the coding interview--Q2.3

    Implement an algorithm to delete a node in the middle of a singly linked list,given only access to t ...

  10. Qt 窗体布局 good

    布局相关对象及简介 窗体上的所有的控件必须有一个合适的尺寸和位置.Qt提供了一些类负责排列窗体上的控件,主要有:QHBoxLayout,QVBoxLayout,QGridLayout,QStackLa ...