列表

详情


NC206674. StrangeBulbs

描述

Christmas is coming. XiaoYang has got a big Christmas tree, and he wants to decorate it.

XiaoYang loves Shiny things. He set up a net of light bulbs on the tree. n light bulbs are connected by m wires. But the bulbs and wires are strange. Each bulb has a switch. If the switch is turned, some bulbs connecting this one will also be switched. The switch operation only flows from the left end to the right end. More precisely, a wire connects u and v so that when the switch of u is turned, the switch of v is turned also. Note that a switch will be turned only once in one step.

Now only the bulb 1 is on. How many steps does it take to turn off all the bulbs?

输入描述

The first line contains two integers n and -- the number of bulbs and wires in the net.

Each of the next m lines contains two space-separated integers u and that mean there's a directed wire connecting the left u and the right v. It's guaranteed that the net is connected, acyclic and doesn't contain any self-loops or multiple edges.


输出描述

One line with an integer denoting the answer.

示例1

输入:

6 7
1 2
1 3
2 4
3 4
2 5
5 6
4 6

输出:

4

说明:

For the first testcase, operations are 1(2,3,4,5,6) and 2(4,5,6) and 3(4,6) and 4(6).

示例2

输入:

4 4
1 2
1 3
2 4
3 4

输出:

4

说明:

For the second testcase, operations are 1(2,3,4) and 2(4) and 3(4) and 4().

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 288ms, 内存消耗: 198764K, 提交时间: 2022-08-28 21:56:03

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
const int N = 4e4+15;
int in[N];
vector<int>E[N];
bitset<N>bt[N];
int n,m;
void topu(){
	int ans=0;
	queue<int>q;
	bt[1][1]=1;
	q.push(1);
	while(!q.empty()){
		int u=q.front();q.pop();
		if(bt[u].count()&1){
			ans++;bt[u][u]=1;
		}
		for(auto v:E[u]){
			in[v]--;
			bt[v]|=bt[u];
			if(!in[v])
				q.push(v);
		}
	}
	cout<<ans<<'\n';
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int u,v;cin>>u>>v;
		E[u].pb(v);in[v]++;
	}
	topu();
}

C++14(g++5.4) 解法, 执行用时: 491ms, 内存消耗: 204500K, 提交时间: 2020-06-18 15:38:05

#include<bits/stdc++.h>
using namespace std;
const int N=4e4+1000;
int n,m,u,v;
vector<int> G[N];
int in[N];
bitset<N> f[N];
int main()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>u>>v;
		G[u].push_back(v);
		in[v]++;
	}
	queue<int> q;
	for(int i=1;i<=n;i++) if(in[i]==0)  q.push(i);
	f[1][1]=1;
	int ans=0;
	while(q.size())
	{
		auto t=q.front();q.pop();
		if(f[t].count()&1) f[t][t]=1,ans++;
		for(auto x: G[t]){
			f[x]|=f[t];
			if(--in[x]==0) q.push(x);
		}
	}
	cout<<ans<<endl;
 } 

C++ 解法, 执行用时: 308ms, 内存消耗: 198784K, 提交时间: 2021-11-01 16:49:53

#include<bits/stdc++.h>
using namespace std;
const int N=4e4+5;
bitset<N>bt[N];
int n,m,ans,du[N];
vector<int>mp[N];
queue<int>p;
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,x,y;i<=m;i++){
		scanf("%d%d",&x,&y);
		mp[x].push_back(y);
        du[y]++;
	}
	p.push(1);
	bt[1][1]=1;
	while(p.size()){
		int x=p.front();
		p.pop();
		if(bt[x].count()&1){
			ans++;
			bt[x][x]=1;
		}
		for(auto y:mp[x]){
			bt[y]|=bt[x];
			if(--du[y]==0)p.push(y);
		}
	}
	printf("%d",ans);
}

上一题