Codeforces Round #143 (Div. 2) (边双联通)

C:

给你一个数列,可以给某个数加上1,最多可以加k次,问操作之后相同的数的个数最多是多少。

排完序可以二分答案是哪个数,也可以直接枚举维护一个左值,每次判断i到j是否可行。

D:

这么简单的题居然不是AB题。。

E:

给你一个无向图,每个点最多只在一个简单环中,问s到t有多少种路径,不能经过同一条边两次,走过的边的集合不同表示路径不同。

思路:

由于每个点最多只在一个简单环中,所以一个点到另一个点经过的环的个数为x则不同路径的种数为 2^x,这个想想还是很明显的。所以对原图进行边双联通缩点后求出树上的lca就可以解决了。

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
using namespace std;
#define pb push_back
typedef __int64 ll;

const int maxn = 100000 + 10;
const int mod = 1000000007 ;

struct Edge {
    int u, to, next, vis;
}edge[maxn<<1];

int head[maxn], dfn[maxn], low[maxn], st[maxn], cnt[maxn], belo[maxn];
int E, Time, top, getot, type;
ll fac[maxn];

struct GEEDGE {
    int u, to;
}geedge[maxn<<1];

void init() {
    memset(head, -1, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    E = Time = top = getot = type = 0;
}

void newedge(int u, int to) {
    edge[E].u = u;
    edge[E].to = to;
    edge[E].vis = 0;
    edge[E].next = head[u];
    head[u] = E++;
}

void tarjan(int u) {
    dfn[u] = low[u] = ++Time;
    st[++top] = u;
    for(int i = head[u];i != -1;i = edge[i].next) {
        if(edge[i].vis) continue;
        edge[i].vis = edge[i^1].vis = 1;
        int to = edge[i].to;
        if(!dfn[to]) {
            tarjan(to);
            low[u] = min(low[u], low[to]);
            if(low[to] > dfn[u]) {
                geedge[getot].u = u;
                geedge[getot++].to = to;
            }
        }
        else
            low[u] = min(low[u], low[to]);
    }
    if(dfn[u] == low[u]) {
        int to;
        type++;
        do {
            cnt[type]++;
            to = st[top--];
            belo[to] = type;
        }while(to != u);
    }
}

int tot, B[maxn<<1], F[maxn<<1], d[maxn<<1][20], pos[maxn<<1], isclr[maxn<<1], qq[maxn];

void RMQ_init(int n) {
    for(int i = 1;i <= n; i++) d[i][0] = B[i];
    for(int j = 1;(1<
       
       
         <= n; j++) for(int i = 1;i+j-1 <= n; i++) d[i][j] = min(d[i][j-1], d[i + (1<<(j-1))][j-1]); } int RMQ(int L, int R) { int k = 0; while((1<<(k+1)) <= R-L+1) k++; return min(d[L][k], d[R-(1< 
        
          pos[b]) swap(a, b); int ans = RMQ(pos[a], pos[b]); return F[ans]; } void dfs(int u) { dfn[u] = ++Time; B[++tot] = dfn[u]; F[Time] = u; pos[u] = tot; for(int i = head[u];i != -1;i = edge[i].next) { int to = edge[i].to; if(!dfn[to]) { if(isclr[to]) qq[to] = qq[u] + 1; else qq[to] = qq[u]; dfs(to); B[++tot] = dfn[u]; } } } int main() { init(); fac[0] = 1; for(int i = 1;i <= 100000; i++) fac[i] = fac[i-1]*2%mod; int n, m, u, to; scanf("%d%d", &n, &m); for(int i = 0;i < m; i++) { scanf("%d%d", &u, &to); newedge(u, to); newedge(to, u); } for(int i = 1;i <= n; i++) if(!dfn[i]) tarjan(i); memset(head, -1, sizeof(head)); memset(dfn, 0, sizeof(dfn)); E = Time = tot = 0; for(int i = 0;i < getot; i++) { u = belo[geedge[i].u]; to = belo[geedge[i].to]; newedge(u, to); newedge(to, u); } for(int i = 1;i <= type; i++) if(cnt[i] > 1) isclr[i] = 1; if(isclr[1]) qq[1] = 1; else qq[1] = 0; dfs(1); RMQ_init(tot); int q, x, y; scanf("%d", &q); while(q--) { scanf("%d%d", &x, &y); x = belo[x]; y = belo[y]; int LCA = lca(x, y), ans; if(LCA == x) { ans = qq[y] - qq[x] + isclr[LCA]; } else if(LCA == y) ans = qq[x] - qq[y] + isclr[LCA]; else ans = qq[x] + qq[y] - qq[LCA]*2 + isclr[LCA]; printf("%I64d\n", fac[ans]); } return 0; } 
         
       
      
      
     
     
    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值