88分求助

P2272 [ZJOI2007] 最大半连通子图

wangtairan114 @ 2024-02-19 14:43:57

WA on #6(听说以前有人错过,但是无法访问)


#include <cstring>
#include <string>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <limits.h>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
using namespace std;
#define min(a,b) ((a)>(b)?(b):(a))
#define max(a,b) ((a)<(b)?(b):(a))
#define INF 0x3f3f3f3f
#define ll long long
#define sc scanf
#define pr printf
#define v1 first
#define v2 second
#define lowbit(x) (x&(-x))
const int MAXN =2000011;
vector<int> g[MAXN];
int dfn[MAXN],low[MAXN],s[MAXN],top,cur;
bool ins[MAXN];
int val[MAXN];
int scc[MAXN],scnt=0;
int x;
void tarjan(int u)
{
    dfn[u]=low[u]=++cur;
    s[++top]=u;
    ins[u]=1;
    for(auto v:g[u])//connected: u->v
    {
        if(!dfn[v])//didn't reach v
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(ins[v])//inside stack
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u])//u is head
    {
        ++scnt;//number of answer
        while(s[top]!=u)
        {
            scc[s[top]]=scnt;
            ins[s[top--]]=0;
        }
        scc[s[top]]=scnt;
        ins[s[top--]]=0;
    }
}
vector<int> v[MAXN];
int n,m,num[MAXN],sum[MAXN];
int dp[MAXN];
int main()
{
    cin >> n>>m>>x;
    for(int i=1; i <= m; i++)
    {
        int x,y;
        sc("%d%d",&x,&y);
        g[x].push_back(y);
    }
    for(int i=1; i <= n; i++)
        if(!dfn[i])
            tarjan(i);
    for(int i=1; i <= n; i++)
    {
        for(int j=0; j < g[i].size(); j++)
        {
            if(scc[g[i][j]]!=0&&scc[g[i][j]]!=scc[i]){
                if(find(v[scc[i]].begin(),v[scc[i]].end(),scc[g[i][j]])==v[scc[i]].end())
                {
                    v[scc[i]].push_back(scc[g[i][j]]);
                    num[scc[g[i][j]]]++;
                }
            }
        }
        val[scc[i]]++;
    }
    queue<int> q;
    for(int i=1; i <= scnt; i++)
    {
        if(num[i]==0)
        {
            q.push(i);
            sum[i]=val[i];
            dp[i]=1;
        }
    }
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=0; i < v[x].size(); i++)
        {
            num[v[x][i]]--;
            if(num[v[x][i]]==0)
            {
                q.push(v[x][i]);
            }
            if(sum[x]+val[v[x][i]]>sum[v[x][i]])
            {
                dp[v[x][i]]=dp[x];
                sum[v[x][i]]=sum[x]+val[v[x][i]];
            }
            else if(sum[x]+val[v[x][i]]==sum[v[x][i]])
            {
                dp[v[x][i]]+=dp[x];
                dp[v[x][i]]%=x;
            }
        }
    }
    int maxnum=0,ans=0;
    for(int i=1; i <= scnt; i++)
    {
        if(sum[i]>sum[maxnum])
        {
            maxnum=i;
            ans=dp[i];
        }
        else if(sum[i]==sum[maxnum])
        {
            ans+=dp[i];
            ans%=x;
        }
    }
//    for(int i=1; i <= scnt; i++)
//    {
//        cout << i << ":";
//        for(int j=0; j < v[i].size(); j++){
//            pr("%d ",v[i][j]);
//        }
//        pr("\n");
//    }
//    for(int i=1; i <= scnt; i++)
//    {
//        cout << dp[i]<<" ";
//    }
    pr("%d\n%d",sum[maxnum],ans);
    return 0;
}

by SJZ2010 @ 2024-02-19 15:01:55

@wangtairan114 缩点后边要去重。


by wangtairan114 @ 2024-02-19 15:11:59

@SJZ2010 去了啊


by jianhe @ 2024-02-19 15:42:57

while(!q.empty()) 这个循环的最后一行。

dp[v[x][i]]%=x;中对 x 取模,但 x 在循环第一行被 int x=q.front(); 了。

@wangtairan114


by wangtairan114 @ 2024-02-19 15:43:28

@jianhe 谢谢


by jianhe @ 2024-02-19 15:43:31


by wangtairan114 @ 2024-02-19 15:45:34

@jianhe 过了。我是**


|