2 条题解

  • 1
    @ 2026-11-9 16:25:25

    考虑限制1是没有什么用的,只要考虑限制2就好了。限制2相当于要求连边的两个点之间有两条不相交的道路,我们发现点双联通分量中每个两点之间都有两条不相交的道路,并且由于我们求出来的点双联通分量之间都是极大的,于是若两个点属于两个不同的点双,那他们之间一定不会连边。并且我们的操作不会使原来不能连边的点对可以连边。于是设我们求出来的点双的大小分别为 c1,c2,c3...ckc_1,c_2,c_3... c_k ,答案即为 ∑i=1kci(ci−1)2\sum_{i=1}^k \frac{c_i(c_i-1)}{2}

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    struct edge{
    	int to,next;
    }a[2000010];
    int head[1000010],ed,dfn[1000010],low[1000010],dfsmax,is[1000010],rt,vis[1000010];
    ll as;
    stack<int>st;
    void add(int x,int y){
    	ed++;
    	a[ed].to=y;
    	a[ed].next=head[x];
    	head[x]=ed;
    }
    void tarjan(int x,int fa)
    {
    	dfsmax++;
    	dfn[x]=dfsmax;
    	low[x]=dfsmax;
    	st.push(x);
    	int sum=0;
    	for(int i=head[x];i!=0;i=a[i].next)
    	{
    		if(dfn[a[i].to]==0){
    			tarjan(a[i].to,0);
    			low[x]=min(low[x],low[a[i].to]);
    			if((low[a[i].to]>=dfn[x]&&rt!=x)||(rt==x&&sum>0)){
    				ll c=0;
    				while(st.top()!=a[i].to){
    					c++;
    					st.pop();
    				}
    				c++;
    				st.pop();
    				as+=c*(c+1)/2;
    				is[x]=1;
    			}
    			sum++;
    		}
    		else if(a[i].to!=fa)
    			low[x]=min(low[x],dfn[a[i].to]);
    	}
    	if(rt==x&&st.empty()==0){
    		ll c=0;
    		while(st.top()!=x){
    			c++;
    			st.pop();
    		}
    		st.pop();
    		c++;
    		as+=c*(c-1)/2;
    		is[x]=1;
    	}
    }
    signed main()
    {
    	freopen("graph.in","r",stdin);
    	freopen("graph.out","w",stdout);
    	ios::sync_with_stdio(0);
    	cin.tie(0);
    	cout.tie(0);
    	int n,m;
    	cin>>n>>m;
    	ed=1;
    	while(m--){
    		int x,y;
    		cin>>x>>y;
    		add(x,y);
    		add(y,x);
    	}
    	rt=1;
    	for(int i=1;i<=n;i++){
    		if(dfn[i]==0){
    			rt=i;
    			tarjan(i,0);
    		}
    	}
    	cout<<as;
    	return 0;
    }
    
    • -2
      @ 2026-11-9 13:11:28

      • 1

      信息

      ID
      107
      时间
      1000ms
      内存
      128MiB
      难度
      7
      标签
      递交数
      78
      已通过
      21
      上传者