2 条题解
-
0

// 拓扑排序+bitset O(n) #include<bits/stdc++.h> using namespace std; const int N=30010; int n,m,rd[N]; vector<int> e[N]; bitset<N> f[N]; //f[i]表示i这个点的可以抵达点的数量,即二进制中1的个数 void topo(){ queue<int> q; for(int i=1; i<=n; i++) if(!rd[i]) q.push(i); while(!q.empty()){ int u=q.front(); q.pop(); f[u][u]=1; //点u的第u位赋1,表示可以到达自己 for(auto v:e[u]){ f[v]|=f[u]; //非重复统计用并集运算 if(--rd[v]==0) q.push(v); } } } int main(){ scanf("%d%d",&n,&m); for(int i=0,x,y; i<m; i++){ scanf("%d%d",&x,&y); e[y].push_back(x); //建反图 rd[x]++; //记录入度 } topo(); for(int i=1; i<=n; i++) printf("%d\n",f[i].count()); } -
0
#include<bits/stdc++.h> using namespace std; const int N=3e4+10; vector<int>G[N]; stack<int>sta; int n,m,din[N]; bitset<N> f[N]; void toposort() { priority_queue<int,vector<int>,greater<int>>q; for(int i=1;i<=n;i++) if( din[i]==0) q.push(i); while(!q.empty()) { int x=q.top();q.pop(); sta.push(x); for(int y:G[x]) if(--din[y]==0) q.push(y); } } int main() { scanf("%d%d",&n,&m); memset(din,0,sizeof(din)); for(int i=1,x,y;i<=m;i++) { scanf("%d%d",&x,&y); G[x].push_back(y); din[y]++; } toposort(); memset(f,0,sizeof(f)); while(!sta.empty()) { int x=sta.top();sta.pop(); f[x][x]=1; for(int y:G[x])f[x]|=f[y]; } for(int i=1;i<=n;i++)printf("%d\n",f[i].count()); return 0; }
- 1
信息
- ID
- 1079
- 时间
- 3000ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- 递交数
- 78
- 已通过
- 43
- 上传者