To solve this problem, we need to determine the number of distinct avatars remaining after (10^{100}) time steps in a group where each member is imitating another member's avatar. Here's a step-by-step explanation of the solution approach:
Understanding the Problem
-
Graph Representation: The scenario can be represented as a directed graph where each node corresponds to a group member, and each directed edge (i \rightarrow a_i) represents member (i) imitating member (a_i).
-
Cycle Detection:
- Nodes that are part of a cycle will keep rotating avatars among themselves indefinitely.
- After (10^{100}) steps, nodes in these cycles will end up holding the avatars from other nodes in the same cycle.
- Non-cyclic nodes will eventually imitate avatars from nodes in cycles.
-
Key Insight: The number of distinct avatars after (10^{100}) steps is equivalent to the number of nodes that are part of cycles in the graph. This is because cycles are the source of distinct avatars that do not get absorbed by other nodes.
Solution Steps
-
Calculate In-degrees:
- Compute the in-degree for each node, which counts how many edges point to that node.
- Nodes with an in-degree of zero cannot be part of any cycle, so they can be disregarded.
-
Identify Cycles Using Topological Sorting:
- Initialize a queue with all nodes having an in-degree of zero.
- Iteratively remove nodes from the queue and decrease the in-degree of their target nodes.
- Nodes remaining in the graph after all zero in-degree nodes are removed are part of cycles.
-
Count Cycle Nodes:
- The number of nodes left after processing is the number of distinct avatars.
Implementation
Here's the C++ implementation based on the above approach:
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
vector<long long> a(n+1);
for(long long i=1;i<=n;i++) cin >> a[i];
vector<int> in_degree(n+1,0);
for(long long i=1;i<=n;i++) in_degree[a[i]]++;
queue<long long> q;
for(long long i=1;i<=n;i++) if(in_degree[i]==0) q.push(i);
// Process the queue
while(!q.empty()){
long long node = q.front(); q.pop();
long long target = a[node];
in_degree[target]--;
if(in_degree[target]==0){
q.push(target);
}
}
// Count nodes with in_degree >0
long long count=0;
for(long long i=1;i<=n;i++) if(in_degree[i]>0) count++;
cout << count;
}
Explanation of the Code
-
Input Parsing:
- The number of members (n) is read.
- An array
a of size (n + 1) is used to store the imitation targets for each member.
-
In-degree Calculation:
- The in-degree of each node is calculated by iterating over the imitation targets.
-
Queue Initialization:
- All nodes with an in-degree of zero are added to a queue. These nodes cannot be part of any cycle.
-
Cycle Detection:
- Nodes are dequeued one by one, and their target's in-degree is decremented.
- If any target's in-degree becomes zero after decrementing, it is added to the queue.
-
Final Count:
- After processing all nodes, any node with an in-degree greater than zero is part of a cycle.
- The number of such nodes is output as the result.
Conclusion
This approach efficiently identifies all nodes that are part of cycles within the group, enabling us to determine the number of distinct avatars after a significant number of imitation steps.