博客
关于我
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 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>