博客
关于我
Jump Conveyor
阅读量:229 次
发布时间: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/

你可能感兴趣的文章
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>
mysql 导入导出大文件
查看>>
MySQL 导出数据
查看>>
mysql 将null转代为0
查看>>
mysql 常用
查看>>
MySQL 常用列类型
查看>>
mysql 常用命令
查看>>
Mysql 常见ALTER TABLE操作
查看>>
MySQL 常见的 9 种优化方法
查看>>