博客
关于我
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 自增id和UUID做主键性能分析,及最优方案
查看>>
Mysql 自定义函数
查看>>
mysql 行转列 列转行
查看>>
Mysql 表分区
查看>>
mysql 表的操作
查看>>
mysql 视图,视图更新删除
查看>>
MySQL 触发器
查看>>
mysql 让所有IP访问数据库
查看>>
mysql 记录的增删改查
查看>>
MySQL 设置数据库的隔离级别
查看>>
MySQL 证明为什么用limit时,offset很大会影响性能
查看>>
Mysql 语句操作索引SQL语句
查看>>
MySQL 误操作后数据恢复(update,delete忘加where条件)
查看>>
MySQL 调优/优化的 101 个建议!
查看>>
mysql 转义字符用法_MySql 转义字符的使用说明
查看>>
mysql 输入密码秒退
查看>>
mysql 递归查找父节点_MySQL递归查询树状表的子节点、父节点具体实现
查看>>
mysql 通过查看mysql 配置参数、状态来优化你的mysql
查看>>
mysql 里对root及普通用户赋权及更改密码的一些命令
查看>>