uva 12442 . Forwarding Emails

本文探讨了一个有趣的短信传播问题,即如何通过特定的转发规则让信息覆盖最多的接收者。文章提供了一种通过深度优先搜索(DFS)来寻找最优解的方法,并讨论了剪枝策略以提高搜索效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

“... so forward this to ten other people, to prove that you believe the emperor has new clothes.”
Aren’t those sorts of emails annoying?
Martians get those sorts of emails too, but they have an innovative way of dealing with them.
Instead of just forwarding them willy-nilly, or not at all, they each pick one other person they know
to email those things to every time - exactly one, no less, no more (and never themselves). Now, the
Martian clan chieftain wants to get an email to start going around, but he stubbornly only wants to
send one email. Being the chieftain, he managed to find out who forwards emails to whom, and he
wants to know: which Martian should he send it to maximize the number of Martians that see it?
Input
The first line of input will contain T (≤ 20) denoting the number of cases.
Each case starts with a line containing an integer N (2 ≤ N ≤ 50000) denoting the number of
Martians in the community. Each of the next N lines contains two integers: u v (1 ≤ u, v ≤ N , u ̸ = v)
meaning that Martian u forwards email to Martian v.
Output
For each case, print the case number and an integer m, where m is the Martian that the chieftain
should send the initial email to. If there is more than one correct answer, output the smallest number.
Sample Input
Sample Output
3
3
1
2
3
4
1
2
4
3
5
1
2
5
3
4
2
3
1
2
1
3
2
2
1
3
4
5
Sample Output
Case 1: 1
Case 2: 4
Case 3: 3

 

题意是说发短信,每个人只会给一个人发,问从哪个人开始发,能传到的人最多

思路是每个人开始做一遍dfs...

毫无意外的TLE了

一个容易想到的剪枝是,如果在第i次之前的路径上的点,在之后以它作为起点遍历一定不优.

我们可以用一个数组vis标记上(注意不要和为了dfs的标记数组vis2混淆,vis2标记的主要作用是判断是否成环)

 

sad,看来还是要提高自己的搜索姿势啊....

/*************************************************************************
    > File Name: code/2015summer/sea#2/B.cpp
    > Author: 111qqz
    > Email: rkz2013@126.com 
    > Created Time: 2015年07月28日 星期二 14时59分16秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)  
typedef long long LL;
typedef unsigned long long ULL;
const int N=5E4+7;
int a[N];
bool vis[N],vis2[N];
int  u,v,n;
int dfs(int x)
{
    int res=0;
    vis2[x]=true;
    int tmp = a[x];
    if (!vis2[tmp])
    {
      res = dfs(tmp)+1; //当前这个认能传的长度=他传的人能传的长度+1
    }
    vis[x] = true;
    vis2[x] = false;
    return res;

    


}
int main()
{
    int T;
    cin>>T;
    int cas = 0;
    while (T--)
    {
      memset(vis,false,sizeof(vis));
      cas++;
      scanf("%d",&n);
      for ( int  i = 0 ; i < n;  i++ )
      {
        scanf("%d %d",&u,&v);
        a[u]=v;
      }
      int mx = - 1;
      int ans;
      for ( int i = 1 ; i <= n ; i++)
      {
    //    memset(vis2,false,sizeof(vis2));
        if (vis[i]) continue;   //如果在上一次的dfs中经过了i,那么从i开始传播一定是之前标记i的那次开始传播的子链,一定不优.
        int tmp = dfs(i);
//        cout<<dfs(i,1)<<endl;
        if (tmp>mx)
        {
            mx = tmp;
            ans = i;
        }
      }
      printf("Case %d: %d\n",cas,ans);
      
    }
  
    return 0;
}

 

转载于:https://www.cnblogs.com/111qqz/p/4683075.html

<think>好的,我现在需要帮助用户解决如何配置net.ipv6.conf.all.forwarding来启用IPv6转发的问题。首先,我需要回顾一下用户提供的引用内容,特别是引用[1]和[2],因为这些部分提到了相关的内核参数和配置步骤。 用户的问题是关于启用IPv6转发,而引用[1]中明确列出了需要设置的参数,比如net.ipv6.conf.all.forwarding=1,以及其他接口的类似设置。引用[2]也提到了设置forwarding参数为1,并且还涉及Docker的IPv6配置。此外,引用[3]虽然主要讨论IPv6地址配置,但可能对理解整体IPv6设置有帮助。 接下来,我需要整理配置IPv6转发的步骤。通常,在Linux系统中,可以通过sysctl命令临时修改内核参数,或者通过修改配置文件使其永久生效。用户提供的引用中提到了两种方法:临时使用sysctl和永久修改sysctl.conf文件。例如,引用[1]中的参数可以直接用sysctl命令设置,而引用[2]中的Docker配置可能需要额外的步骤。 需要注意的是,用户可能是在不同的环境中配置IPv6转发,比如物理机、虚拟机或容器。特别是引用[2]提到Docker需要启用IPv6并设置子网,这可能用户的问题相关,如果用户是在Docker环境中配置的话。因此,在回答中应该包含常规的配置方法,并提及Docker相关的注意事项。 另外,用户提供的示例中引用了三个来源,因此回答中需要正确引用这些来源。比如,设置forwarding参数的部分应引用[1]和[2],而Docker配置部分引用[2]。 可能还需要验证配置是否生效,比如使用sysctl检查参数值,或者查看/proc/sys下的文件内容。此外,防火墙设置(如ip6tables)可能会影响转发功能,需要提醒用户检查防火墙规则,但用户提供的引用中没有提到这部分,可能需要补充。 最后,生成的相关问题需要围绕IPv6转发的配置和应用展开,比如如何验证是否生效、持久化配置的方法、Docker中的IPv6配置等。</think>### 配置IPv6转发的步骤 #### 1. **临时启用IPv6转发** 通过`sysctl`命令可临时修改内核参数,重启后失效: ```bash sudo sysctl -w net.ipv6.conf.all.forwarding=1 sudo sysctl -w net.ipv6.conf.default.forwarding=1 # 若需指定接口(如eth0、eth1),单独设置: sudo sysctl -w net.ipv6.conf.eth0.forwarding=1 sudo sysctl -w net.ipv6.conf.eth1.forwarding=1 ``` #### 2. **永久启用IPv6转发** 修改`sysctl.conf`配置文件,使设置持久化: ```bash echo "net.ipv6.conf.all.forwarding=1" | sudo tee -a /etc/sysctl.conf echo "net.ipv6.conf.default.forwarding=1" | sudo tee -a /etc/sysctl.conf # 应用配置 sudo sysctl -p ``` #### 3. **容器环境(如Docker)的特殊配置** 若需在Docker中启用IPv6,需额外操作[^2]: 1. 编辑Docker配置文件`/etc/docker/daemon.json`,添加以下内容: ```json { "ipv6": true, "fixed-cidr-v6": "2001:db8:1::/64" # 替换为实际IPv6子网 } ``` 2. 重载Docker配置: ```bash sudo systemctl reload docker ``` 3. **注意**:容器内的应用(如MongoDB)需支持IPv6协议栈。 #### 4. **验证配置是否生效** ```bash # 检查内核参数值 sysctl net.ipv6.conf.all.forwarding # 输出应为:net.ipv6.conf.all.forwarding = 1 ``` ### 关键配置说明 - `net.ipv6.conf.all.forwarding=1`:启用所有接口的IPv6转发功能[^1]。 - `accept_ra=2`(可选):允许接收路由器通告(需结合转发需求)。 - **NATIPv6**:IPv6通常不依赖NAT,因其地址空间足够大[^3],但可通过`ip6tables`实现特定转发规则。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值