game 竞赛图 缩环

【问题背景】 zhx 和他的妹子(们)做游戏。 【问题描述】 考虑 N 个人玩一个游戏, 任意两个人之间进行一场游戏 (共 N*(N-1)/2 场) , 且每场一定能分出胜负。 现在,你需要在其中找到三个人构成“剪刀石头步”局面:三个人 A,B,C 满足 A 战胜 B,B 战胜 C,C 战胜 A。 【输入格式】 第一行一个正整数 N,表示参加游戏的人数。 接下来 N 行,每行 N 个数 0/1,中间没有空格隔开。第 i 行第 j 列数字为 1 表示 i 在游戏中战胜了 j。所有对角线元素(即第 i 行第 i 个元素)为 0,保证数 据合法。 【输出格式】 如果存在三个人构成“剪刀石头布”局面,输出三个人的编号(从 1 开始) 。 如果不存在这样的三个人,输出一个数-1。 【样例输入】 5 00100 10000 01001 11101 11000 【样例输出】 1 3 2 【数据规模与约定】 55。 80%的数据,1 ≤ ? ≤ 10。 对于100%的数据,1 ≤ ? ≤ 5000。
其实就是,把一个竞赛图慢慢缩小,直到只剩3个点。

#include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include<iostream> using namespace std; #define M 5009 bool map[M][M]; int cycle[M],len; int stake[M],vis[M],spos[M],stop; int n; char c; bool dfs(int x) { vis[x]=1,stake[stop++]=x,spos[x]=stop-1; for(int i=0;i<n;i++) if(map[x][i]) { if(vis[i]==1) { len=stop-spos[i]; memcpy(cycle,stake+spos[i],sizeof (int) * len); return 1; } else if(vis[i]==0) { bool v=dfs(i); if(v) return 1; } } --stop,vis[x]=2; return 0; } void out() { for(int i=1;i<len-1;i++) if (map[cycle[i+1]][cycle[0]]) { printf("%d %d %d\n", cycle[0]+1, cycle[i]+1, cycle[i+1]+1); } } int main() { freopen("game.in","r",stdin); freopen("game.ans","w",stdout); scanf("%d",&n); char temp[M]; for(int i=0;i<n;i++) { scanf("\n%s", temp); for(int j=0;j<n;j++) map[i][j]= (bool) temp[j]=='1'; } for(int i=0;i<n;i++) if(!vis[i]) { bool find =dfs(i); if(find) { out(); return 0; } } cout<<-1; return 0; }

#include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include<iostream> #define M 5009 bool map[M][M]; int cycle[M],len; int stake[M],vis[M],spos[M],stop; int n; char c; bool dfs(int x) { vis[x]=1,stake[stop++]=x,spos[x]=stop-1; for(int i=0;i<n;i++) if(map[x][i]) { if(vis[i]==1) { len=stop-spos[i]; memcpy(cycle,stake+spos[i],sizeof (int) * len); return 1; } else if(vis[i]==0) { bool v=dfs(i); if(v) return 1; } } --stop,vis[x]=2; return 0; } void out() { for(int i=1;i<len-1;i++) if (map[cycle[i+1]][cycle[0]]) { printf("%d %d %d\n", cycle[0]+1, cycle[i]+1, cycle[i+1]+1); } } int main() { freopen("game.in","r",stdin); freopen("game.ans","w",stdout); scanf("%d",&n); char temp[M]; for(int i=0;i<n;i++) { scanf("\n%s", temp); for(int j=0;j<n;j++) map[i][j]= temp[j]=='1'; } for(int i=0;i<n;i++) if(!vis[i]) { bool find =dfs(i); if(find) { out(); return 0; } } printf("-1"); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App