10.17T1 联通块

缺钱的LZY

Problem BackGround

Lzy是一名优秀的中学生,这天,他又开始肝Azur Line了。在手游中,如何能够变得更 强,唯有氪金,Lzy深谙此道。正巧,Azur Line国庆礼包上线了,Lzy的忍痛割手,决定使用他的 多年积蓄。

众所周知,Lzy是一个聪明屁事多的好孩子,他把他所有的存钱罐的钥匙放入了他的存钱罐中,他 必须打开所有存钱罐以获得足够的钱,每个存钱罐可以被相应的钥匙打开或者被砸开,

但是,Lzy太优秀了,他深知砸开存钱罐是对广大劳动人民的极大不敬,所以,他想,在打开所有 存钱罐时,砸开最少的存钱罐。

由于Lzy还要忙着去Azur Line3-4,这个简单的问题就交给你了。

 

ProblemDescription

Lzyn个存钱罐,Lzy已经将所有钥匙放入了一些存钱罐中,现在Lzy知道他每个钥匙所 在的存钱罐,Lzy想知道他最少砸几个存钱罐才能打开所有存钱罐。

 

Input

第一行一个整数n

第二行n个整数,第i个整数ai表示Lzyi个存钱罐的钥匙放在了第ai个存钱罐中

Ouput

一个整数表示Lzy最少砸几个存钱罐才能打开所有存钱罐

 

 

 

SampleInput

4

2

1

2

4

Sample Output

2

 

Data Scale

测试点

n

1~2

20

3~6

5000

7~9

1000000

10

5000000

Hint

选手下发文件提供了FastIO

 

 

 

此题本身就个裸的求联通块,但是LZY卡读入,所以只能写FastIO

code:

 

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #define N 10000005
  5 using namespace std;
  6 namespace fastIO{
  7     #define BUF_SIZE 100000
  8     #define OUT_SIZE 100000
  9     #define ll long long
 10     //fread->read
 11     bool IOerror=0;
 12     inline char nc(){
 13         static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
 14         if (p1==pend){
 15             p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);
 16             if (pend==p1){IOerror=1;return -1;}
 17             //{printf("IO error!\n");system("pause");for (;;);exit(0);}
 18         }
 19         return *p1++;
 20     }
 21     inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
 22     inline void read(int &x){
 23         bool sign=0; char ch=nc(); x=0;
 24         for (;blank(ch);ch=nc());
 25         if (IOerror)return;
 26         if (ch=='-')sign=1,ch=nc();
 27         for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
 28         if (sign)x=-x;
 29     }
 30     inline void read(ll &x){
 31         bool sign=0; char ch=nc(); x=0;
 32         for (;blank(ch);ch=nc());
 33         if (IOerror)return;
 34         if (ch=='-')sign=1,ch=nc();
 35         for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
 36         if (sign)x=-x;
 37     }
 38     inline void read(double &x){
 39         bool sign=0; char ch=nc(); x=0;
 40         for (;blank(ch);ch=nc());
 41         if (IOerror)return;
 42         if (ch=='-')sign=1,ch=nc();
 43         for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
 44         if (ch=='.'){
 45             double tmp=1; ch=nc();
 46             for (;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0');
 47         }
 48         if (sign)x=-x;
 49     }
 50     inline void read(char *s){
 51         char ch=nc();
 52         for (;blank(ch);ch=nc());
 53         if (IOerror)return;
 54         for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
 55         *s=0;
 56     }
 57     inline void read(char &c){
 58         for (c=nc();blank(c);c=nc());
 59         if (IOerror){c=-1;return;}
 60     }
 61     //getchar->read
 62     inline void read1(int &x){
 63         char ch;int bo=0;x=0;
 64         for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1;
 65         for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
 66         if (bo)x=-x;
 67     }
 68     inline void read1(ll &x){
 69         char ch;int bo=0;x=0;
 70         for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1;
 71         for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
 72         if (bo)x=-x;
 73     }
 74     inline void read1(double &x){
 75         char ch;int bo=0;x=0;
 76         for (ch=getchar();ch<'0'||ch>'9';ch=getchar())if (ch=='-')bo=1;
 77         for (;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
 78         if (ch=='.'){
 79             double tmp=1;
 80             for (ch=getchar();ch>='0'&&ch<='9';tmp/=10.0,x+=tmp*(ch-'0'),ch=getchar());
 81         }
 82         if (bo)x=-x;
 83     }
 84     inline void read1(char *s){
 85         char ch=getchar();
 86         for (;blank(ch);ch=getchar());
 87         for (;!blank(ch);ch=getchar())*s++=ch;
 88         *s=0;
 89     }
 90     inline void read1(char &c){for (c=getchar();blank(c);c=getchar());}
 91     //scanf->read
 92     inline void read2(int &x){scanf("%d",&x);}
 93     inline void read2(ll &x){
 94         #ifdef _WIN32
 95             scanf("%I64d",&x);
 96         #else
 97         #ifdef __linux
 98             scanf("%lld",&x);
 99         #else
100             puts("error:can't recognize the system!");
101         #endif
102         #endif
103     }
104     inline void read2(double &x){scanf("%lf",&x);}
105     inline void read2(char *s){scanf("%s",s);}
106     inline void read2(char &c){scanf(" %c",&c);}
107     inline void readln2(char *s){gets(s);}
108     //fwrite->write
109     struct Ostream_fwrite{
110         char *buf,*p1,*pend;
111         Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;}
112         void out(char ch){
113             if (p1==pend){
114                 fwrite(buf,1,BUF_SIZE,stdout);p1=buf;
115             }
116             *p1++=ch;
117         }
118         void print(int x){
119             static char s[15],*s1;s1=s;
120             if (!x)*s1++='0';if (x<0)out('-'),x=-x;
121             while(x)*s1++=x%10+'0',x/=10;
122             while(s1--!=s)out(*s1);
123         }
124         void println(int x){
125             static char s[15],*s1;s1=s;
126             if (!x)*s1++='0';if (x<0)out('-'),x=-x;
127             while(x)*s1++=x%10+'0',x/=10;
128             while(s1--!=s)out(*s1); out('\n');
129         }
130         void print(ll x){
131             static char s[25],*s1;s1=s;
132             if (!x)*s1++='0';if (x<0)out('-'),x=-x;
133             while(x)*s1++=x%10+'0',x/=10;
134             while(s1--!=s)out(*s1);
135         }
136         void println(ll x){
137             static char s[25],*s1;s1=s;
138             if (!x)*s1++='0';if (x<0)out('-'),x=-x;
139             while(x)*s1++=x%10+'0',x/=10;
140             while(s1--!=s)out(*s1); out('\n');
141         }
142         void print(double x,int y){
143             static ll mul[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,
144                 1000000000,10000000000LL,100000000000LL,1000000000000LL,10000000000000LL,
145                 100000000000000LL,1000000000000000LL,10000000000000000LL,100000000000000000LL};
146             if (x<-1e-12)out('-'),x=-x;x*=mul[y];
147             ll x1=(ll)floor(x); if (x-floor(x)>=0.5)++x1;
148             ll x2=x1/mul[y],x3=x1-x2*mul[y]; print(x2);
149             if (y>0){out('.'); for (size_t i=1;i<y&&x3*mul[i]<mul[y];out('0'),++i); print(x3);}
150         }
151         void println(double x,int y){print(x,y);out('\n');}
152         void print(char *s){while (*s)out(*s++);}
153         void println(char *s){while (*s)out(*s++);out('\n');}
154         void flush(){if (p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}}
155         ~Ostream_fwrite(){flush();}
156     }Ostream;
157     inline void print(int x){Ostream.print(x);}
158     inline void println(int x){Ostream.println(x);}
159     inline void print(char x){Ostream.out(x);}
160     inline void println(char x){Ostream.out(x);Ostream.out('\n');}
161     inline void print(ll x){Ostream.print(x);}
162     inline void println(ll x){Ostream.println(x);}
163     inline void print(double x,int y){Ostream.print(x,y);}
164     inline void println(double x,int y){Ostream.println(x,y);}
165     inline void print(char *s){Ostream.print(s);}
166     inline void println(char *s){Ostream.println(s);}
167     inline void println(){Ostream.out('\n');}
168     inline void flush(){Ostream.flush();}
169     //puts->write
170     char Out[OUT_SIZE],*o=Out;
171     inline void print1(int x){
172         static char buf[15];
173         char *p1=buf;if (!x)*p1++='0';if (x<0)*o++='-',x=-x;
174         while(x)*p1++=x%10+'0',x/=10;
175         while(p1--!=buf)*o++=*p1;
176     }
177     inline void println1(int x){print1(x);*o++='\n';}
178     inline void print1(ll x){
179         static char buf[25];
180         char *p1=buf;if (!x)*p1++='0';if (x<0)*o++='-',x=-x;
181         while(x)*p1++=x%10+'0',x/=10;
182         while(p1--!=buf)*o++=*p1;
183     }
184     inline void println1(ll x){print1(x);*o++='\n';}
185     inline void print1(char c){*o++=c;}
186     inline void println1(char c){*o++=c;*o++='\n';}
187     inline void print1(char *s){while (*s)*o++=*s++;}
188     inline void println1(char *s){print1(s);*o++='\n';}
189     inline void println1(){*o++='\n';}
190     inline void flush1(){if (o!=Out){if (*(o-1)=='\n')*--o=0;puts(Out);}}
191     struct puts_write{
192         ~puts_write(){flush1();}
193     }_puts;
194     inline void print2(int x){printf("%d",x);}
195     inline void println2(int x){printf("%d\n",x);}
196     inline void print2(char x){printf("%c",x);}
197     inline void println2(char x){printf("%c\n",x);}
198     inline void print2(ll x){
199         #ifdef _WIN32
200             printf("%I64d",x);
201         #else
202         #ifdef __linux
203             printf("%lld",x);
204         #else
205             puts("error:can't recognize the system!");
206         #endif
207         #endif
208     }
209     inline void println2(ll x){print2(x);printf("\n");}
210     inline void println2(){printf("\n");}
211     #undef ll
212     #undef OUT_SIZE
213     #undef BUF_SIZE
214 };
215 using namespace fastIO;
216 struct node{
217     int u,v;
218 }e[N];
219 int vis[N],first[N],nxt[N],cnt;
220 inline void add(int u,int v){
221     e[++cnt].u=u;
222     e[cnt].v=v;
223     nxt[cnt]=first[u];
224     first[u]=cnt;
225 }
226 inline void dfs(int x){
227     vis[x]=1;
228     for(int i=first[x];i;i=nxt[i]){
229         int v=e[i].v;
230         if(vis[v])continue;
231         dfs(v);
232     }
233 }
234 int main(){
235 //    freopen("lzy.in","r",stdin);
236 //    freopen("lzy.out","w",stdout);
237     int n;
238     read(n);
239     for(int i=1;i<=n;++i){
240         int x;
241         read(x);
242         add(i,x);
243         add(x,i);
244     }
245     int ans=0;
246     for(int i=1;i<=n;++i){
247         if(vis[i])continue;
248         ans++;
249         dfs(i);
250     }
251     cout<<ans;
252     return 0;
253 }

 

over

 

posted @ 2018-10-17 15:30  saionjisekai  阅读(108)  评论(0编辑  收藏  举报