• Source
    1. /**************************************************************
    2.   Problem: 3060
    3.   User: zrts
    4.   Language: C++
    5.   Result: Accepted
    6.   Time:12912 ms
    7.   Memory:39868 kb
    8. ****************************************************************/
    9.  
    10. #include <cstdio>
    11. #include <cstring>
    12. #include <algorithm>
    13. // by zrt
    14. // problem:
    15. // 无论你在什么时候开始,重要的是开始以后就不要停止。
    16. using namespace std ;
    17. typedef long long LL ;
    18. const double eps(1e-10) ;
    19. const int inf(0x3f3f3f3f) ;
    20. int f[1000005],n,m,k;
    21. int get(int x){
    22. return f[x]==x?x:f[x]=get(f[x]);
    23. }
    24. int H[1000005],X[4000005],P[4000005],tot;
    25. inline void add(int x,int y){
    26. P[++tot]=y;X[tot]=H[x];H[x]=tot;
    27. }
    28. int main(){
    29. #ifdef LOCAL
    30. freopen("in.txt","r",stdin) ;
    31. freopen("out.txt","w",stdout) ;
    32. #endif
    33. scanf("%d%d%d",&n,&m,&k);
    34. for(int i=1;i<=n;i++) f[i]=i;
    35. for(int i=0,x,y;i<m;i++){
    36. scanf("%d%d",&x,&y);
    37. add(x,y);
    38. }
    39. for(int i=k+1;i<=n;i++){
    40. for(int j=H[i];j;j=X[j]){
    41. if(P[j]>k){
    42. f[get(i)]=get(P[j]);
    43. }
    44. }
    45. }
    46. int top(0);
    47. for(int i=1;i<=n;i++){
    48. for(int j=H[i];j;j=X[j]){
    49. if((i<=k||P[j]<=k)){
    50. if(get(i)==get(P[j])){
    51. top++;
    52. }else{
    53. f[get(i)]=get(P[j]);
    54. }
    55. }
    56. }
    57. }
    58. printf("%d\n",top);
    59. return 0 ;
    60. }