zoukankan      html  css  js  c++  java
  • F

     题目链接:https://cn.vjudge.net/contest/280949#problem/F

    题目大意:给你n个人,然后给你m个关系,每个关系输入t1, t2 .代表t1和t2是朋友关系(双向关系)。然后输入一个k,代表两个人是亲密的朋友关系的话,就至少有k个共同的朋友,然后问你题目中这样的朋友有多少对?

    具体思路:注意一个地方,朋友关系具有传递性,打个比方 t1和t2 变成了亲密的朋友,然后t0 本来和t1是朋友关系,但是和t2不是朋友关系,t1和t2成为朋友之后,t0也就和t2称为朋友了。

    (读题还是太慢了。。。。)

    AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 # define inf 0x3f3f3f3f
     4 const int maxn = 100+100;
     5 int vis[maxn][maxn];
     6 int n,m,k;
     7 bool cal(int t1,int t2)
     8 {
     9     int num=0;
    10     for(int i=1; i<=n; i++)
    11     {
    12         if(vis[t1][i]&&vis[t2][i])
    13         {
    14             num++;
    15         }
    16     }
    17     if(num<k)
    18         return false;
    19     //  cout<<t1<<" "<<t2<<endl;
    20     return true;
    21 }
    22 int main()
    23 {
    24     int T;
    25     scanf("%d",&T);
    26     while(T--)
    27     {
    28         memset(vis,0,sizeof(vis));
    29         int t1,t2;
    30         scanf("%d %d %d",&n,&m,&k);
    31         for(int i=1; i<=m; i++)
    32         {
    33             scanf("%d %d",&t1,&t2);
    34             t1++;
    35             t2++;
    36             vis[t1][t2]=1;
    37             vis[t2][t1]=1;
    38         }
    39         int flag=1;
    40         int num=0;
    41         while(flag)//不能一次循环就完事了,还会有经过传递之后成为新朋友的关系。
    42         {
    43             int ans=0;
    44             flag=0;
    45             for(int i=1; i<=n; i++)
    46             {
    47                 for(int j=i+1; j<=n; j++)
    48                 {
    49                     if(vis[i][j])
    50                         continue;
    51                     if(cal(i,j))
    52                     {
    53                         vis[i][j]=vis[j][i]=1;
    54                         ans++;
    55                         flag=1;
    56                     }
    57                 }
    58             }
    59             num+=ans;
    60         }
    61         printf("%d
    ",num);
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    nginx能访问html静态文件但无法访问php文件
    LeetCode "498. Diagonal Traverse"
    LeetCode "Teemo Attacking"
    LeetCode "501. Find Mode in Binary Search Tree"
    LeetCode "483. Smallest Good Base" !!
    LeetCode "467. Unique Substrings in Wraparound String" !!
    LeetCode "437. Path Sum III"
    LeetCode "454. 4Sum II"
    LeetCode "445. Add Two Numbers II"
    LeetCode "486. Predict the Winner" !!
  • 原文地址:https://www.cnblogs.com/letlifestop/p/10316366.html
Copyright © 2011-2022 走看看