首页 > 其他分享 >Codeforces Round #244 (Div. 2) C. Checkposts

Codeforces Round #244 (Div. 2) C. Checkposts

时间:2023-02-05 22:02:57浏览次数:28  
标签:cnt long int Checkposts Scc 244 maxn cnt2 Div

裸的tarjan

依题意有向图上i和j之间能互相到达,i和j肯定在同一个scc内

最小的代价就是Σ每个scc内最小的cost

方案就是每个scc内最小值的数的乘积

#include<bits/stdc++.h>
using namespace std;
const long long mod=1000000007;
const int maxn=int(1e5)+7;
int n,m,s,num=0,step=0,Scc=0,dp[maxn],rd[maxn],weight[maxn],scc[maxn],dfn[maxn],flag[maxn],low[maxn],stap[maxn];
long long minscc[maxn],p[maxn],w[maxn];
struct lys{
    int from,to,next;
}e[maxn*7];
int cnt=0,head[maxn*7];
void add(int from,int to){
    cnt++;e[cnt].from=from;e[cnt].to=to;e[cnt].next=head[from];head[from]=cnt;
}
struct lys2{
    int from,to,next;
}e2[maxn*7];
int cnt2=0,head2[maxn*7];
void add2(int from,int to){
    cnt2++;e2[cnt2].from=from;e2[cnt2].to=to;e2[cnt2].next=head2[from];head2[from]=cnt2;
}
void init(){
    memset(flag,0,sizeof(flag));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(stap,0,sizeof(stap));
    for(int i=1;i<=n;i++) scc[i]=i,minscc[i]=mod;
}
void tarjan(int u){   flag[u]=1;
    dfn[u]=low[u]=++step;
    stap[++num]=u;//压栈
    for (int i=head[u];i;i=e[i].next){
        int v=e[i].to;
        if (dfn[v]==0)
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if (flag[v]==1)
            low[u]=min(low[u],dfn[v]);
    }
    int v;
    if (dfn[u]==low[u])
    { 
        Scc++;//联通块标号
        int cnt=0;
        vector<int>tmp;
        do{
            v=stap[num--];
            flag[v]=0;
            scc[v]=Scc;//v属于第Scc个联通块
            minscc[Scc]=min(minscc[Scc],w[v]);
            tmp.push_back(w[v]);
            if(u==v) continue;
        }
        while (v!=u);
        for(auto v:tmp){
            if(v==minscc[Scc]) p[Scc]++;
        }
    }
}
queue<int>q;
void out( )
{    
   long long ans=0;
   for(int i=1;i<=Scc;i++) ans=(ans+minscc[i]);
   long long way=1;
   for(int i=1;i<=Scc;i++) way=way*p[i]%mod;
   cout<<ans<<" "<<way%mod;
}
int main( )
{
    //freopen("lys.in","r",stdin);
    cin>>n;
    init();
    for(int i=1;i<=n;i++) cin>>w[i];
    cin>>m;
    for(int i=1;i<=m;i++){
        int a,b;cin>>a>>b;
        add(a,b);
    }

    for(int i=1;i<=n;i++)
        if(!dfn[i]) tarjan(i);
    
     out();
}

 

标签:cnt,long,int,Checkposts,Scc,244,maxn,cnt2,Div
From: https://www.cnblogs.com/liyishui2003/p/17094021.html

相关文章

  • Codeforces Round #848 (Div. 2)D. Flexible String Revisit-dp、初等数学
    题目:https://codeforces.com/problemset/problem/1778/D场内打的,首先很容易想到答案来自于a、b不同的位置有几个,设\(f_k\)表示当前有k个不同的位置要复原到完全一样需要多......
  • Codeforces Round #840 (Div. 2) E. Node Pairs-图论、小dp
    题目链接:https://codeforces.com/problemset/problem/1763/E题意有点绕,大概就是给一个p,现在希望找到一个n个点的有向图G,恰好有p个点对\(1\lequ<v\leqn\)使得\(u\tov\)......
  • Codeforces Round #849 (Div. 4) A~G1
    欢乐场hhA.询问给定串是否是codeforces的子串voidsolve(){strings="codeforces";stringa;cin>>a;string::size_typeidx;idx=s.find(a);/......
  • 2.3 Codeforces Round #849 (Div. 4)
    记录一下第一次可以写到G1,只剩一道题就可以ak,虽然是div4,不过也值得开心一下。A-CodeforcesCheckingvoidsolve(){ charc; cin>>c; strings="codeforces"; ......
  • Educational Codeforces Round 142 (Rated for Div. 2)(C,D)
    EducationalCodeforcesRound142(RatedforDiv.2)(C,D)CC这道题的大意是题目大意是给你一个任意的排序,我们要把这个排序通过任意个操作变成一个有序的排序操作是,选择......
  • Codeforces Round #838 (Div. 2)-D. GCD Queries-GCD、交互
    题目:https://codeforces.com/problemset/problem/1762/D有一个0~n-1的排列,你要在至多2n次询问中找到两个位置x,y,使得\(p_x,p_y\)至少有一者为0.每次询问可以问两个不同的i......
  • Codeforces Round #661 (Div. 3)
    A.RemoveSmallest题意:给定一个序列,每次操作可以任选两个差的绝对值小于等于排序后计算相邻数的差,只要有大于AC代码:constintN=2e5+50;intn,m;inta[N];intmain......
  • Codeforces Round #662 (Div. 2)
    A.RainbowDash,FluttershyandChessColoring题意:有手动写几个找找规律。AC代码:intn,m;intmain(){intT;sd(T);while(T--){sd(n);pd(n/2+1);......
  • Codeforces Round #658 (Div. 2)
    ACommonSubsequence只要找到有一个相同的元素输出即可。AC代码:constintN=1010;inta[N],b[N];intans;intcnt[N];intmain(){intt;sd(t);while(t--){......
  • Codeforces Round #657 (Div. 2)
    A.AcaciusandString题意:给你一个串,你可以把换成任意字符,使得这个串最后只出现一次暴力枚举AC代码:strings;intn;stringT="abacaba";boolcheck(string&a){int......