题目链接:https://codeforces.com/contest/1270/problem/G
【题意】
一序列数a1,a2,…,an,满足 i−n≤ai≤i−1 (1≤i≤n)
求这个序列的一个子集,使子集的和为0
【思路】
i−n≤ai≤i−1
∴ 1<=i-ai<=n
构建一个图,对每个ai,建一条从 i 到 i−ai 的边,由于每个点都有出度,整个图必定有环。
找到这个环,环上的ai之和为0.
example:
1-a1=2
2-a2=3
3-a3=1
把3个等式加起来得a1+a2+a3=0;
建边:1指向2,2指向3,3指向1
【坑】
不要忘记初始化
每个点都有一个出边,而且指向1~n内,所以找到一个环就可以停下了(首尾相接)
t的范围是(1~1e6)如果每次初始化都用memset,就是O(n)的,就T掉了,所以初始化要用for循环
找环直接判断首位相接,不要用tarjan
初始化不要用memset!
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int t,n,a[maxn],to[maxn],vis[maxn],sta[maxn],top;
vector<int> g;
int main() {
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
g.clear();
for(int i = 1; i <= n; i++)
vis[i] = 0,to[i] = 0;
for(int i = 1; i <= n; i++) {
scanf("%d",&a[i]);
a[i] = i - a[i];
to[i] = a[i];
}
int p = 1,top = 0;
while(!vis[p]) {
sta[++top] = p;
vis[p] = 1;
p = to[p];
}
do{
g.push_back(sta[top]);
}while(sta[top--] != p);
printf("%d\n",g.size());
for(int i = 0; i < g.size(); i++) {
printf("%d%s",g[i],i == g.size() - 1 ? "\n" : " ");
}
}
}
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define LL long long
#define pii pair<int,int>
#define mp(a,b) make_pair(a,b)
const int MAXN = 1e6+10;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
int a[MAXN],head[MAXN],tot,ans; bool vis[MAXN];
struct node{ int v,nxt; }edge[MAXN<<1];
inline void add(int u,int v){
edge[++tot].v=v; edge[tot].nxt=head[u]; head[u]=tot;
}
void dfs(int u){
vis[u]=1;
for(int i=head[u];i;i=edge[i].nxt){
int v = edge[i].v;
if(vis[v]){ ans = v; return; }
dfs(v);
}
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
#endif // ONLINE_JUDGE
int T; scanf("%d",&T);
while(T--){
int n; scanf("%d",&n); tot=0;
for(int i=1;i<=n;i++) vis[i]=0,head[i]=0;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) add(i,i-a[i]);
dfs(1); vector<int> res;
int u=ans-a[ans]; res.push_back(ans);
while(u!=ans){
res.push_back(u);
u = u-a[u];
}
printf("%d\n",res.size());
for(int i=0;i<res.size();i++) printf("%d ",res[i]);
puts("");
}
return 0;
}