关于我的代码有地方写错了却仍然ac的情况
查看原帖
关于我的代码有地方写错了却仍然ac的情况
287539
勇敢的菜鸡楼主2020/10/2 23:58

数据真水

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+100;
typedef long long LL;
vector<LL>g[maxn];
stack<LL>s;
LL n,m;
LL low[maxn],dfn[maxn],col[maxn],times=0,cnt=0,du[maxn],fa[maxn];
bool inq[maxn],ma[5010][5010];
void tarjan2(LL u)
{
    dfn[u]=low[u]=++times;
    s.push(u);inq[u]=true;
    for(LL i=0;i<g[u].size();i++){
        LL to=g[u][i];fa[to]=u;///这里的节点更新错了..fa[to]=u应该放到下面的if里面去的
        if(!dfn[to]){
            tarjan2(to);
            if(low[to]>dfn[u])
            {
                  ///  cout<<u<<" "<<to<<endl;
            }
            low[u]=min(low[u],low[to]);
        }
        else if(to!=fa[u])low[u]=min(low[u],dfn[to]);
    }
    if(low[u]==dfn[u])
    {
        cnt++;
        LL y;
        do{
           y=s.top();
           inq[y]=false;
           col[y]=cnt;
           s.pop();
        }while(y!=u);
        return;
    }
}
 
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  cin>>n>>m;
  memset(fa,-1,sizeof(fa));
  for(LL i=1;i<=m;i++)
  {
     LL u,v;cin>>u>>v;
     if(ma[u][v]) continue;
     ma[u][v]=ma[v][u]=true;
     g[u].push_back(v);g[v].push_back(u);
 
  }
  for(LL i=1;i<=n;i++){
 
    if(!dfn[i]) tarjan2(i);
  }
 
  for(LL i=1;i<=n;i++){
    for(LL j=0;j<g[i].size();j++)
    {
        if(col[i]!=col[g[i][j]]) du[col[i]]++;
    }
  }
 
  LL sum=0;
  for(LL i=1;i<=cnt;i++){
   /// cout<<du[i]<<endl;
    if(du[i]==1) sum++;
  }
  cout<<(sum+1)/2<<endl;
  return 0;
}
2020/10/2 23:58
加载中...