[USACO06JAN] 冗余路径Redundant Paths

博客介绍了一种算法,先用Tarjan处理出low数组,将边双连通分量缩成点形成树,通过连接叶子节点使树变成边双联通分量,给出最优策略及答案计算方式,即设叶子节点数为nop,答案为(ans + 1)/2,还提到代码中缩点统计点在树中的出度与入度之和。

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

题目链接

算法:

        首先用Tarjan处理出low数组,然后考虑将每一个边双连通分量缩成一个点,根据其定义,这将形成一棵树,那我么只要在树上加最少的边使这棵树也变成一个边双联通分量,那么,这个最优策略就是:连接若干对叶子节点,如果还剩余一个节点,那我们将它和另外任意一个叶子节点相连即可。

        也就是说,设nop为叶子节点数,那么所求答案即为(ans+1)/2。

        (对于缩点,代码中没有直接连边,而是统计出了每一个点,在树中的出度+入度之和)

 

Code:

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
template<typename T> void read(T &num){
    char c=getchar();num=0;T f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){num=(num<<3)+(num<<1)+(c^48);c=getchar();}
    num*=f;
}
template<typename T> void qwq(T x){
    if(x>9)qwq(x/10);
    putchar(x%10+'0');
}
template<typename T> void write(T x){
    if(x<0){x=-x;putchar('-');}
    qwq(x);putchar('\n');
}
template<typename T> void chkmin(T &x,T y){x=x<y?x:y;}
struct wzy{
    int nxt,vertice;
}edge[20010];
int head[5010];int len=0;
inline void add_edge(int x,int y){
    edge[++len].nxt=head[x];edge[len].vertice=y;head[x]=len;return;
}

bool in[5010][5010];
int dfn[5010];int low[5010];int tot=0;int deg[5010];
inline void tarjan(int son,int father){
    dfn[son]=low[son]=++tot;
    for(int i=head[son];i;i=edge[i].nxt){
        int nop=edge[i].vertice;
        if(nop==father)continue;
        if(!dfn[nop]){
            tarjan(nop,son);chkmin(low[son],low[nop]);
        }else{
            chkmin(low[son],dfn[nop]);
        }
    }
    return;
}

int main(){
    int n,r;read(n);read(r);
    rep(i,1,r){
        int u,v;read(u);read(v);
        if(in[u][v])continue;
        in[u][v]=1;add_edge(u,v);add_edge(v,u);
    }
    
    tarjan(1,0);
    rep(i,1,n){
        for(int j=head[i];j;j=edge[j].nxt){
            if(low[i]!=low[edge[j].vertice]){
                deg[low[i]]++;deg[low[edge[j].vertice]]++;
            }
        }
    }
    
    int ans=0;
    rep(i,1,n){ans+=(deg[i]==2);}
    write((ans+1)/2);
    return 0;
}

 

### USACO 2016 January Contest Subsequences Summing to Sevens Problem Solution and Explanation In this problem from the USACO contest, one is tasked with finding the size of the largest contiguous subsequence where the sum of elements (IDs) within that subsequence is divisible by seven. The input consists of an array representing cow IDs, and the goal is to determine how many cows are part of the longest sequence meeting these criteria; if no valid sequences exist, zero should be returned. To solve this challenge efficiently without checking all possible subsequences explicitly—which would lead to poor performance—a more sophisticated approach using prefix sums modulo 7 can be applied[^1]. By maintaining a record of seen remainders when dividing cumulative totals up until each point in the list by 7 along with their earliest occurrence index, it becomes feasible to identify qualifying segments quickly whenever another instance of any remainder reappears later on during iteration through the dataset[^2]. For implementation purposes: - Initialize variables `max_length` set initially at 0 for tracking maximum length found so far. - Use dictionary or similar structure named `remainder_positions`, starting off only knowing position `-1` maps to remainder `0`. - Iterate over given numbers while updating current_sum % 7 as you go. - Check whether updated value already exists inside your tracker (`remainder_positions`). If yes, compare distance between now versus stored location against max_length variable's content—update accordingly if greater than previous best result noted down previously. - Finally add entry into mapping table linking latest encountered modulus outcome back towards its corresponding spot within enumeration process just completed successfully after loop ends normally. Below shows Python code implementing described logic effectively handling edge cases gracefully too: ```python def find_largest_subsequence_divisible_by_seven(cow_ids): max_length = 0 remainder_positions = {0: -1} current_sum = 0 for i, id_value in enumerate(cow_ids): current_sum += id_value mod_result = current_sum % 7 if mod_result not in remainder_positions: remainder_positions[mod_result] = i else: start_index = remainder_positions[mod_result] segment_size = i - start_index if segment_size > max_length: max_length = segment_size return max_length ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值