Friends ZOJ 3710

Friends

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Alice lives in the country where people like to make friends. The friendship is bidirectional and if any two person have no less than k friends in common, they will become friends in several days. Currently, there are totally n people in the country, and m friendship among them. Assume that any new friendship is made only when they have sufficient friends in common mentioned above, you are to tell how many new friendship are made after a sufficiently long time.

Input

There are multiple test cases.

The first lien of the input contains an integer T (about 100) indicating the number of test cases. Then T cases follow. For each case, the first line contains three integers n, m, k (1 ≤ n ≤ 100, 0 ≤ mn×(n-1)/2, 0 ≤ kn, there will be no duplicated friendship) followed by m lines showing the current friendship. The ith friendship contains two integers ui, vi (0 ≤ ui, vi < n, ui ≠ vi) indicating there is friendship between person ui and vi.

Note: The edges in test data are generated randomly.

Output

For each case, print one line containing the answer.

Sample Input

3
4 4 2
0 1
0 2
1 3
2 3
5 5 2
0 1
1 2
2 3
3 4
4 0
5 6 2
0 1
1 2
2 3
3 4
4 0
2 0

Sample Output

2
0
4

題解:題目大意,T組數據,N個朋友,M個友誼關係,關鍵值K,接下來M行A,B。表示A,b是好友,任意兩個人之間只要共同好友數大於等於K,則他們成爲新朋友,求,經過一段時間後,新朋友的數目……

我一開始用的結構體,但忘了用while,結果,一直錯,加上while之後,過了,但看了看別人的代碼,發現我寫的好複雜……


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<string>
#include<math.h>
#include<map>
#include<queue>
#include<stack>
#define INF 0x3f3f3f3f
#define ll long long
#define For(i,a,b) for(int i=a;i<b;i++)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
struct node
{
    int a,b[105];              // b 存與x[i]是朋友關係的人,a 存b 的大小
} x[105];
int c[205];
int  v[105][105];              // 做下標記
int main()
{
    int t,n,m,M;
    scanf("%d",&t);
    while(t--)
    {
        mem(v,0);
        scanf("%d%d%d",&n,&m,&M);
        for(int i=0; i<n; i++)
            x[i].a=0;
        int aa,bb;
        For(i,0,m)
        {
            scanf("%d%d",&aa,&bb);
            x[aa].b[x[aa].a++]=bb;
            x[bb].b[x[bb].a++]=aa;
            v[aa][bb]=v[bb][aa]=1;
        }
        int ans=0,falg;
        while(1)                          // 防止新朋友出現後,依據新朋友又出現了新朋友
        {
            falg=0;
            for(int i=0; i<n; i++)
            {
                if(x[i].a<M)
                    continue;
                for(int j=0; j<n; j++)
                {
                    if(j==i)
                        continue;
                    if(v[i][j]||v[j][i])
                        continue;
                    if(x[j].a<M)
                        continue;
                    mem(c,0);
                    sort(x[i].b,x[i].b+x[i].a);
                    int aaa=unique(x[i].b,x[i].b+x[i].a)-x[i].b;
                    for(int k=0; k<aaa; k++)
                        c[k]=x[i].b[k];
                    sort(x[j].b,x[j].b+x[j].a);
                    int bbb=unique(x[j].b,x[j].b+x[j].a)-x[j].b;
                    for(int k=aaa; k<aaa+bbb; k++)
                        c[k]=x[j].b[k-aaa];
                    sort(c,c+aaa+bbb);
                    int d=unique(c,c+aaa+bbb)-c;
                    if(aaa+bbb-d>=M)
                    {
                        x[i].b[x[i].a++]=j;
                        x[j].b[x[j].a++]=i;
                        v[i][j]=v[j][i]=1;
                        ans++;
                        falg=1;
                    }
                }
            }
            if(!falg)break;
        }
        printf("%d\n",ans);
    }
}



下面是傳說中的人家的代碼……


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8

#define fre(i,a,b)  for(i = a; i <= b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 1000000000
#define N 105

int  n,m,num,ans;
int a[N][N];

void solve()
{
    int i,j,cnt;
    ans=0;


    while(1)
    {
        bool flag=false;


        for(i=0; i<n; i++)
            for(j=0; j<n; j++)
            {
                if(i==j) continue;
                if(a[i][j]) continue;

                int cnt=0;
                for(int k=0; k<n; k++)
                    if(a[i][k]&&a[k][j])
                        cnt++;

                if(cnt>=num)
                {
                    a[i][j]=a[j][i]=1;
                    ans++;
                    flag=true;
                }

            }

        if(!flag)  break;
    }
    printf("%d\n",ans);
}

int main()
{
    int i,j,t;
    ans=0;
    sf(t);
    while(t--)
    {
        sfff(n,m,num);
        mem(a,0);
        int s,e;

        while(m--)
        {
            sff(s,e);
            a[s][e]=a[e][s]=1;
        }

        solve();

    }
    return 0;
}


還是做得題太少了,思路太僵化了,哎,努力吧……



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章