poj Popular Cows(tarjan +缩点)

本文探讨了在一个包含N头牛的群体中,通过M对有序对来评估每头牛的流行度,利用Tarjan算法进行点的合并和强连通分支的识别,最终确定哪些牛被所有其他牛认为是最受欢迎的。通过实例输入,展示了如何计算并理解特定牛的高受欢迎度。

Language:
Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 24384 Accepted: 10007

Description

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. 

Input

* Line 1: Two space-separated integers, N and M 

* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular. 

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 

Source



题意: n头牛相互羡慕,可以传递羡慕,问被所有牛羡慕的牛的最大数目


思路:

 tarjan缩点,把不同的点规划到一个强联通分支,最后判断强连通分支出度为0的点是否唯一,唯一则有解,答案是该强连通分支的点的数目,否者无解


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define N 10024

int head[N],low[N],time[N],e_num,tim_num;
int n,m,ans,instack[N],vis[N],type[N],out[N];

struct stud{
  int to,next;
}e[N*10];

stack<int>q;

inline void add(int u,int v)
{
    e[e_num].to=v;
    e[e_num].next=head[u];
    head[u]=e_num++;
}

void tarjan(int x)
{
    int i,j;
    q.push(x);
    instack[x]=1;

    time[x]=low[x]=++tim_num;

    for(i=head[x];i!=-1;i=e[i].next)
    {
        int to=e[i].to;
        if(time[to]==0)
        {
            tarjan(to);
            if(low[x]>low[to]) low[x]=low[to];
        }
        else
            if(instack[to]&&low[x]>time[to])
                low[x]=time[to];
    }

    if(low[x]==time[x])
    {
        ans++;
        do{
            j=q.top();
            q.pop();
            instack[j]=0;
            type[j]=ans;   //标记j 属于第几个联通分支
            vis[ans]++;    //这个联通分支的点+1
           // printf("%d ans=%d\n",j,ans);
        }while(j!=x);
    }
}

void solve()
{
    int i,j;
    mem(time,0);
    mem(instack,0);
    mem(out,0);
    mem(vis,0);

    ans=tim_num=0;

    for(i=1;i<=n;i++)
        if(time[i]==0)
          tarjan(i);

	for(i=1;i<=n;i++)
		for(j=head[i];j!=-1;j=e[j].next)
	{
		int to=e[j].to;

		if(type[i]!=type[to])
			{
				out[type[i]]=1;  //标记有出度的联通分支
			}
	}

	int t=0,pos;

	for(i=1;i<=ans;i++)    //一共有ans个联通分支,取出其中
		if(out[i]==0)
	{
		t++;
		pos=i;
		if(t>1) break;
	}

   //如果出度为0的联通分支只有一个,那么这个联通分支的个数就是答案,
   //否则,无解
	if(t==1)
		printf("%d\n",vis[pos]);
	else
		printf("0\n");


}

int main()
{
    int i,j;
    scanf("%d%d",&n,&m);
    {
        int u,v;
        e_num=0;

        mem(head,-1);

        while(m--)
        {
            sff(u,v);
            add(u,v);
        }
        solve();
    }
    return 0;
}






先看效果: https://renmaiwang.cn/s/jkhfz Hue系列产品将具备高度的个性化定制能力,并且借助内置红、蓝、绿三原色LED的灯泡,能够混合生成1600万种不同色彩的灯光。 整个操作流程完全由安装于iPhone上的应用程序进行管理。 这一创新举措为智能照明控制领域带来了新的启示,国内相关领域的从业者也积极投身于相关研究。 鉴于Hue产品采用WiFi无线连接方式,而国内WiFi网络尚未全面覆盖,本研究选择应用更为普及的蓝牙技术,通过手机蓝牙与单片机进行数据交互,进而产生可调节占空比的PWM信号,以此来控制LED驱动电路,实现LED的调光功能以及DIY调色方案。 本文重阐述了一种基于手机蓝牙通信的LED灯设计方案,该方案受到飞利浦Hue智能灯泡的启发,但考虑到国内WiFi网络的覆盖限制,故而选用更为通用的蓝牙技术。 以下为相关技术细节的详尽介绍:1. **智能照明控制系统**:智能照明控制系统允许用户借助手机应用程序实现远程控制照明设备,提供个性化的调光及色彩调整功能。 飞利浦Hue作为行业领先者,通过红、蓝、绿三原色LED的混合,能够呈现1600万种颜色,实现了全面的定制化体验。 2. **蓝牙通信技术**:蓝牙技术是一种低成本、短距离的无线传输方案,工作于2.4GHz ISM频段,具备即插即用和抗干扰能力。 蓝牙协议栈由硬件层和软件层构成,提供通用访问Profile、服务发现应用Profile以及串口Profiles等丰富功能,确保不同设备间的良好互操作性。 3. **脉冲宽度调制调光**:脉冲宽度调制(PWM)是一种高效能的调光方式,通过调节脉冲宽度来控制LED的亮度。 当PWM频率超过200Hz时,人眼无法察觉明显的闪烁现象。 占空比指的...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值