| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #include <bits/stdc++.h> |
| using namespace std; |
| using pii=pair<int,int>; |
| #define hr cout<<"-------------------------------------\n" |
| #define br cout<<'\n'; |
| const int N=5005; |
| |
| int n; |
| bool g[N][N]; |
| |
| int h[N<<1],ne[N*N],e[N*N],tot; |
| void add(int a,int b) { |
| e[++tot]=b; ne[tot]=h[a]; h[a]=tot; |
| } |
| |
| int dfn[N<<1],low[N<<1],cnt; |
| int color[N<<1],scnt; |
| stack<int>st; |
| bool vis[N<<1]; |
| |
| void tarjan(int now) { |
| dfn[now]=low[now]=++cnt; |
| st.push(now); |
| vis[now]=1; |
| for(int i=h[now];i;i=ne[i]) { |
| int to=e[i]; |
| if(dfn[to]==0) { |
| tarjan(to); |
| low[now]=min(low[now],low[to]); |
| } |
| else if(vis[to])low[now]=min(low[now],dfn[to]); |
| } |
| if(dfn[now]==low[now]) { |
| scnt++; |
| while(1) { |
| int k=st.top();st.pop();vis[k]=0; |
| color[k]=scnt; |
| if(k==now)break; |
| } |
| } |
| } |
| |
| |
| int c1[N],c2[N],cnt1=0,cnt2=0; |
| int match[N],f[N]; |
| bool t[N]; |
| |
| void calc() { |
| for(int i=1;i<=n;i++) { |
| if(color[i]==color[i+n]) { |
| cout<<"0\n"; |
| return ; |
| } |
| if(color[i]<color[i+n])c1[++cnt1]=i,t[i]=1; |
| else c2[++cnt2]=i; |
| } |
| int ans=(cnt1&&cnt2); |
| for(int i=1;i<=cnt1;i++)for(int j=1;j<=cnt2;j++) |
| if(g[c1[i]][c2[j]])f[c1[i]]++,match[c1[i]]=c2[j]; |
| for(int i=1;i<=cnt2;i++)for(int j=1;j<=cnt1;j++) |
| if(g[c2[i]][c1[j]]==0)f[c2[i]]++,match[c2[i]]=c1[j]; |
| for(int i=1;i<=n;i++)if(f[i]==1&&f[match[i]]==0)ans++; |
| for(int i=1;i<=n;i++)if(f[i]==0) |
| if((t[i]&&cnt1>1)||(t[i]==0&&cnt2>1))ans++; |
| cout<<ans<<'\n'; |
| } |
| |
| void solve() { |
| cin>>n; |
| for(int i=1,cnt,x;i<=n;i++) { |
| cin>>cnt; |
| while(cnt--)cin>>x,g[i][x]=1; |
| } |
| for(int i=1;i<=n;i++)for(int j=1;j<=n;j++) |
| if(i!=j) { |
| if(g[i][j])add(i+n,j); |
| else add(i,j+n); |
| } |
| for(int i=1;i<=2*n;i++)if(dfn[i]==0)tarjan(i); |
| calc(); |
| } |
| |
| signed main() { |
| ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); |
| solve(); |
| return 0; |
| } |
| |
| |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!