进食后人,哦不,警示后人
查看原帖
进食后人,哦不,警示后人
554576
yueluoxingchen楼主2024/7/22 17:43

我的这份代码不开O2过了,开O2TLE两个点。

关键这题是默认打开O2,害得我调一下午

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#define int long long
using namespace std;

const int N=5000005; 
int n,m,num,cnt,count,idx;
long long ans,sum;
int dfn[N],low[N];
bool iscut[N];
struct node{
	int u,v,nxt;
}g[N<<1];
int hd[N];
stack<int> stk;
vector<int> ds[N];

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0' || ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0' && ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

inline void write(int x)
{
    if (!x)
    {
        putchar('0');
        return;
    }
    int len = 0, k1 = x, c[40];
    if (k1 < 0) k1 = -k1, putchar('-');
    while (k1) c[len ++ ] = k1 % 10 ^ 48, k1 /= 10;
    while (len -- ) putchar(c[len]);
}

void add(int u,int v)
{
	g[++cnt]={u,v,hd[u]};
	hd[u]=cnt;
}
 
void Tarjan(int now,int father)
{
	dfn[now]=low[now]=++idx;
	int child=0;
	stk.push(now);
	if(now==father && g[now].nxt==0)
	{
		ds[++count].push_back(now);
		return;
	}
	for(int i=hd[now];i;i=g[i].nxt)
	{
		int v=g[i].v;
		if(!dfn[v])
		{
			child++;
			Tarjan(v,now);
			if(low[v]<low[now]) low[now]=low[v];
			if(low[v]>=dfn[now])
			{
				iscut[now]=true;
				count++;
				ds[count].push_back(now);
				int p;
				do{
					p=stk.top();
					ds[count].push_back(p);
					stk.pop();
				}while(p!=v);
			}
		}else if(dfn[v]<low[now] && v!=father) low[now]=dfn[v];
	}
	if(father==0 && child==1) iscut[now]=false;
	if(father==0 && child==0)
	{
		count++;
		ds[count].push_back(now);
	}
	return;
}
 
signed main() {
	n=read();
	m=read();
	for(int i=1;i<=m;i++)
	{
		int x=read();
		int y=read();
		if(x==y) continue;
		add(x,y);
		add(y,x);
	}
	for(int i=1;i<=n;i++) if(!dfn[i]) Tarjan(i,0);
	write(count);
	printf("\n");
	for(int i=1;i<=count;i++)
	{
		write(ds[i].size());
		printf(" ");
		for(int j=0;j<ds[i].size();j++)
		{
			write(ds[i][j]);
			printf(" ");
		}
		printf("\n");
	}
	return 0;
}

不开O2

开O2

2024/7/22 17:43
加载中...