Henan 2014 provincial Race world's gateway topological sorting

Source: Internet
Author: User

10404:e. World of William Time Limit:2 Sec Memory limit:128 MB
Submit:2 solved:1
[Submit] [Status] [Web Board] Description

an empire has a N a new type of weapon known as the "World's Gateway". Now for the sake of the country's economic development, it needs a lot of money, and for this reason, the Imperial President OBM is ready to sell some weapons to other countries.

The president of this Empire OBM know, it N may be restricted to each other. For example, the 3 weapons would defeat the first 1 weapons, and the first 4 weapons would defeat section 3 Kinds of weapons and so on. He hoped that at least one of the weapons sold was in his hands, with the aim that the Empire could restrain the rest of the world, so that it could maintain its hegemonic position on the Earth.

but the President OBM the mind is a bit stupid, he wants to know how many weapons he can sell, would you like to help him?

( You can tell the computer, but don't tell him.) )

Input

First line: K indicates how many sets of test data are available.

Next to each set of test data:

1 Span style= "font-size:14px" > line :        N                  new type

2~n+1 line: A 1   a 2 a n      i a i

1≤k≤10 0≤n≤106 1≤ai≤n

Output

for each set of test data, the output takes one line: The maximum number of weapons that can be sold

Sample Input
172 3 1 3 6 5 4
Sample Output
3
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack > #include <climits> #include <cstring> #include <cmath> #include <map> #include <set># Define INF 100000000using namespace Std;int a[100005],d[100005],vis[100005];int main () {int t;cin >> t;while (T--) { int n;scanf ("%d", &n);p riority_queue<int,vector<int>,greater<int> > Que;memset (d,0,sizeof (d) ); memset (vis,0,sizeof (VIS)); int ans = 0,num = 0;for (int i = 1;i <= n;i++) {scanf ("%d", &a[i]); if (a[i] = = i) {num++;} D[a[i]] + +;} for (int i = 1;i <= n;i++) {if (d[i] = = 0) {Que.push (i);}} while (!que.empty ()) {int p = que.top (); Que.pop (); ans + = 1;vis[p] = 1;if (Vis[a[p]]) {num + = 1;continue;} D[A[A[P]] [--;vis[a[p]] = 1;if (D[a[a[p]] = = 0) {Que.push (P);} num + = 2;} Ans + = (n-num)/2;printf ("%d\n", ans);} return 0;}


Henan 2014 provincial Race world's gateway topological sorting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.