Codeforces Round #318 (Div. 2)

<pre name="code" class="cpp">

思路:最暴力的想法,用一个三重循环去遍历,这道题给出的时间是2秒,而且cf的服务器肯定很好,所以时间上应该是没有压力的,但我心里一直没底,总感觉会超时。

在这里,看了大牛们的代码,学了一个新思路,对于一个三重循环,可以找出他们两两之间有什么关系,用pair,或strcut保存下来,题目里的m是4000,远远小于n,这样会使时间压力减少很多,

62ms

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=5005;
int g[maxn][maxn];
int degree[maxn];
int main(void)
{
    int n,m;
    scanf("%d%d",&n,&m);
    memset(degree,0,sizeof(degree));
    memset(g,0,sizeof(g));
    for(int i=1;i<=m;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        g[a][b]=1;
        g[b][a]=1;
        degree[a]++;
        degree[b]++;
    }
    int minn=inf;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(g[i][j])
            {
                for(int k=j+1;k<=n;k++)
                {
                    if(g[i][k]&&g[j][k])
                    {
                        minn=min(minn,degree[i]+degree[j]+degree[k]-6);
                    }
                }
            }
        }
    }
    if(minn!=inf)
    printf("%d\n",minn);
    else printf("-1\n");
    return 0;
}

31ms

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=5005;
int g[maxn][maxn];
int degree[maxn];
int main(void)
{
    int n,m;
    scanf("%d%d",&n,&m);
    memset(degree,0,sizeof(degree));
    memset(g,0,sizeof(g));
    for(int i=1;i<=m;i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        g[a][b]=1;
        g[b][a]=1;
        degree[a]++;
        degree[b]++;
    }
    int minn=inf;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(g[i][j])
            {
                for(int k=j+1;k<=n;k++)
                {
                    if(g[i][k]&&g[j][k])
                    {
                        minn=min(minn,degree[i]+degree[j]+degree[k]-6);
                    }
                }
            }
        }
    }
    if(minn!=inf)
    printf("%d\n",minn);
    else printf("-1\n");
    return 0;
}

版权声明:转载务必请标明出处,谢谢

时间: 2024-08-02 15:48:09

Codeforces Round #318 (Div. 2)的相关文章

在青岛穷游打的cf codeforces Round #318 (Div. 2) A.Bear and Elections

这场cf是暑假集训后在青岛旅游打的一场,好累..... 题意:给出一个序列,要使a[1]大于a[2]~a[n],a[1]每次可以增加一,这个一从a[2]到a[[n]里面任意一个数减一扣除,求最少的步数 思路:要不断地修改值,并从中找出最大的,可以想到优先队列,还要保存下该数的编号,要知道在队首时是a[1].还有处里一种特殊情况,详见代码 总结:这道题并不难,我用了40多分钟,主要前面太急了,一开始并没有想到是优先队列,是一些其他的想法,只要合适一个样例就下手去做,导致有很明显的bug我竟然敲完代

Codeforces Round #318 (Div. 2) B Bear and Three Musketeers

不要想多了直接暴.对于u枚举a和b,判断一个是否连边,更新答案. #include<bits/stdc++.h> using namespace std; int n,m; const int maxn = 4001; #define PB push_back vector<int> G[maxn]; bool g[maxn][maxn]; int deg[maxn]; const int INF = 0x3f3f3f3f; int main() { //freopen("

Codeforces Round #318 (Div. 2) A Bear and Elections

优先队列模拟一下就好. #include<bits/stdc++.h> using namespace std; priority_queue<int>q; int main() { int n; scanf("%d",&n); int t; scanf("%d",&t); for(int i = 2; i <= n; i++){ int v; scanf("%d",&v); q.push(v

Codeforces Round #318 (Div. 2) C Bear and Poker

很简单,求一下所有数的2和3的幂是任意调整的,把2和3的因子除掉以后必须相等. #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5+5; ll a[maxn]; int main() { //freopen("in.txt","r",stdin); int n; scanf("%d",&n); for(i

Codeforces Round #318 (Div. 2) D Bear and Blocks

不难发现在一次操作以后,hi=min(hi-1,hi-1,hi+1),迭代这个式子得到k次操作以后hi=min(hi-j-(k-j),hi-k,hi+j-(k-j)),j = 1,2,3... 当k == min(hi-j+j,hi+j+j)时hi会变成0,因为min具有传递性,那么可以左右分开来考虑,hi-j+j化一下就是hi-j-(i-j)+i,其中后面i和j无关,所以只要找出前面最小的hi-i 对于右边的类似处理,最后再扫一遍,得出最大的一个k就是答案. #include<bits/std

Codeforces Round #318 (Div. 2) A、B、C

574A - Bear and Elections 题意: 输入一个数字n,接着输入n个正整数. 题目要求第一个整数要大于其余整数,其余整数每次可以减小1并增加到第一个数字中. 问至少要多少次才能满足要求. 思路: 用优先队列维护一下就可以了. #include <stdio.h> #include <algorithm> #include <iostream> #include <string.h> #include <queue> using

Codeforces Round #318(Div. 2)(A,B,C,D)

A题: 题目地址:Bear and Elections 题意:最少变换多少次可以使得第一个数字大于后面所有数字. 思路:把后面n-1个数排序,让第一个和最后一个数比较,然后增减.知道第一个数大于最后一个数为止 #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <sstream>

Codeforces Round #318(Div 1) 573A, 573B,573C

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 这场的前两题完全是手速题...A题写了7分钟,交的时候已经500+了,好在B题出的速度勉强凑活吧,and C题也没有FST A. Bear and Poker Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n pla

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/