博客
关于我
Jump Conveyor
阅读量:227 次
发布时间:2019-02-28

本文共 1701 字,大约阅读时间需要 5 分钟。

解题思路:思维性的搜索。搜索时给点标记3,如果还搜到了3,那么就说明构成了一个环,给环标记上2。搜索时如果遇到一个环,那么也说明可以构建一个环,继续返回2,如果没有搜索到环,那么就给标记上1.

#include
using namespace std;typedef long long ll;typedef long double lf;typedef unsigned long long ull;typedef pair
P;const int inf = 0x7f7f7f7f;const ll INF = 1e16;const int N = 1e6+10;const ull base = 131;const ll mod = 1e9+7;const double PI = acos(-1.0);const double eps = 1e-4;inline int read(){ int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){ x=x*10+ch-'0';ch=getchar();}return x*f;}inline string readstring(){ string str;char s=getchar();while(s==' '||s=='\n'||s=='\r'){ s=getchar();}while(s!=' '&&s!='\n'&&s!='\r'){ str+=s;s=getchar();}return str;}int random(int n){ return (int)(rand()*rand())%n;}void writestring(string s){ int n = s.size();for(int i = 0;i < n;i++){ printf("%c",s[i]);}}int a[N],vis[N];int n;int dfs(int i){ if(i < 1 || i > n) return 0; if(vis[i]==1) return 0; if(vis[i] == 3 || vis[i] == 2) return 2; vis[i] = 3; i = i+a[i]; if(dfs(i)){ vis[i] = 2; return 2; }else { if(i<1||i>n) return 0; vis[i] = 1; return 0; }}void solve(){ n = read(); for(int i = 1;i <= n;i++){ a[i] = read();vis[i] = 0; } for(int i = 1;i <= n;i++){ if(dfs(i)){ vis[i] = 2; }else { vis[i] = 1; } } int ans = 0; for(int i = 1;i <= n;i++){ if(vis[i] == 2) ans++; } printf("%d\n",ans);}int main(){ //freopen("out.txt","w",stdout); //srand((unsigned)time(NULL)); int t = read(); while(t--){ solve(); } return 0;}

转载地址:http://pkqp.baihongyu.com/

你可能感兴趣的文章
201604-4 游戏 ccf
查看>>
1144. The Missing Number (20)
查看>>
为什么阿里巴巴不建议在for循环中使用”+”进行字符串拼接
查看>>
【Spring Boot 26】分别在SpringBoot和Vue中解决跨域问题
查看>>
Class.forName(),classloader.loadclass用法详解
查看>>
tp5.1 页面错误!请稍后再试~ 安装好后,提示错误
查看>>
阿里云 安全组规则 设置某个IP不能访问服务器(出站)
查看>>
系统打了补丁后,IIS装不了的解决…
查看>>
禁止重复提交(JavaScript控制表单…
查看>>
php js 通过sotitle(id,arr)函数输入ID取得返回值
查看>>
删除外键约束
查看>>
c++ 预处理命令 #error 用法
查看>>
OpenGL fragmentlist片段列表的实例
查看>>
OpenGL hdrb和loom的实例
查看>>
OpenGL packetbuffer分组缓冲器的实例
查看>>
OpenGL shader class自定义着色器的实例
查看>>
OpenGL textures combined组合纹理的实例
查看>>
C语言打印字符串的所有排列组合(附完整源码)
查看>>
Qt Creator编码
查看>>
Qt Designer的UI文件格式
查看>>