QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#345883#7860. Graph of Maximum Degree 3ushg8877RE 0ms12044kbC++142.7kb2024-03-07 16:37:272024-03-07 16:37:32

Judging History

你现在查看的是最新测评结果

  • [2024-03-07 16:37:32]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:12044kb
  • [2024-03-07 16:37:27]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MP make_pair
const int MAXN=1e5+5;
int n,m;ll ans=0;
vector<int> edg[MAXN][2],chn;
int p[MAXN],c[MAXN],tot,cnt;bool vis[MAXN];
struct node{int mn,cnt;};
node operator +(node x,node y){
	node z;
	z.mn=min(x.mn,y.mn);z.cnt=0;
	if(z.mn==x.mn) z.cnt+=x.cnt;
	if(z.mn==y.mn) z.cnt+=y.cnt;
	return z;
}
void operator +=(node &x,int y){
	x.mn+=y;
}
struct segt{
node val[MAXN<<2];int laz[MAXN<<2];
void build(int id=1,int l=0,int r=m-1){
	laz[id]=0;
	if(l==r) {val[id]=(node){l,1};return;}
	int mid=l+r>>1;
	build(id<<1,l,mid);build(id<<1|1,mid+1,r);
	val[id]=val[id<<1]+val[id<<1|1];
}
void pushdown(int id){
	laz[id<<1]+=laz[id],laz[id<<1|1]+=laz[id];
	val[id<<1]+=laz[id];val[id<<1|1]+=laz[id];
	laz[id]=0;
}
void add(int L,int R,int x,int id=1,int l=0,int r=m-1){
	if(L>R) return;
	if(L<=l&&r<=R){
		laz[id]+=x;val[id]+=x;
		return;
	}
	pushdown(id);
	int mid=l+r>>1;
	if(L<=mid) add(L,R,x,id<<1,l,mid);
	if(mid<R) add(L,R,x,id<<1|1,mid+1,r);
	val[id]=val[id<<1]+val[id<<1|1];
}
node ask(int L,int R,int id=1,int l=0,int r=m-1){
	if(L<=l&&r<=R) return val[id];
	pushdown(id);
	int mid=l+r>>1;
	if(R<=mid) return ask(L,R,id<<1,l,mid);
	if(mid<L) return ask(L,R,id<<1|1,mid+1,r);
	return ask(L,R,id<<1,l,mid)+ask(L,R,id<<1|1,mid+1,r);
}
}T;
void dfs(int u,int fa){
	p[u]=++tot;c[u]=cnt;
	for(int v:edg[u][0]) if(v!=fa){
		dfs(v,u);
	}
}
void dfs1(int u,int fa){
	chn.push_back(u);vis[u]=true;
	for(int v:edg[u][1]) if(v!=fa){
		dfs1(v,u);
	}
}
void solve(){
	static int stl[MAXN],str[MAXN],topl,topr,l;
	topl=topr=0;stl[0]=str[0]=-1;
	m=chn.size(),l=0;T.build();
	for(int i=0;i<m;i++){
		if(i!=0&&c[chn[i]]!=c[chn[i-1]]) l=i;
		int lst=i;
		while(topl&&p[chn[stl[topl]]]>p[chn[i]]){
			T.add(stl[topl-1]+1,lst-1,p[chn[stl[topl]]]-p[chn[i]]);
			lst=stl[topl-1]+1,topl--;
		}
		stl[++topl]=i;
		lst=i;
		while(topr&&p[chn[str[topr]]]<p[chn[i]]){
			T.add(str[topr-1]+1,lst-1,p[chn[i]]-p[chn[str[topr]]]);
			lst=str[topr-1]+1,topr--;
		}
		str[++topr]=i;
		ans+=T.ask(l,i).cnt;
	}
}
int main(){
	ios::sync_with_stdio(false);
	// freopen("Otomachi_Una.in","r",stdin);
	// freopen("Otomachi_Una.out","w",stdout);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		int u,v,c;cin>>u>>v>>c;
		edg[u][c].push_back(v);
		edg[v][c].push_back(u);
	}
	for(int i=1;i<=n;i++) if(edg[i][0].empty()||edg[i][1].empty()){
		edg[i][0].clear();edg[i][1].clear();
		assert(0);
	}
	for(int i=1;i<=n;i++) if(edg[i][0].size()<=1&&!p[i]) cnt++,dfs(i,0);
	for(int i=1;i<=n;i++) if(edg[i][1].size()<=1&&!vis[i]){
		chn.clear();dfs1(i,0);
		solve();
	}
	cout<<ans%998244353<<'\n';
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 10860kb

input:

3 4
1 2 0
1 3 1
2 3 0
2 3 1

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: 0
Accepted
time: 0ms
memory: 12044kb

input:

4 6
1 2 0
2 3 0
3 4 0
1 4 1
2 4 1
1 3 1

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: -100
Runtime Error

input:

20 28
9 6 1
9 6 0
3 8 0
8 4 0
3 8 1
3 4 1
2 13 0
13 1 0
19 1 0
2 1 1
2 19 1
13 19 1
14 15 1
14 15 0
7 12 0
12 17 0
20 17 0
7 17 1
7 20 1
12 20 1
16 18 0
18 10 0
5 10 0
16 10 1
16 5 1
18 5 1
4 6 0
9 11 0

output:


result: