今日SGU 5.27

SGU 122

题意:给你n个人,每个人有大于 N / 2(向上取整)的朋友,问你1这个人有一个书,每个人都想看,只能从朋友之间传递,然后最后回到了1这个人,问你

是否有解,然后有解输出路径

收获:哈密尔顿路

一:Dirac定理(充分条件)

  设一个无向图中有N个顶点,若所有顶点的度数大于等于N/2,则哈密顿回路一定存在.(N/2指的是⌈N/2⌉,向上取整)

二:基本的必要条件

  设图G=<V, E>是哈密顿图,则对于v的任意一个非空子集S,若以|S|表示S中元素的数目,G-S表示G中删除了S中的点以及这些点所关联的边后得到的子图,则W(G-S)<=|S|成立.其中W(G-S)是G-S中联通分支数.

三:竞赛图(哈密顿通路)

  N(N>=2)阶竞赛图一点存在哈密顿通路.

还偷了一个哈密尔顿回路模板:https://blog.csdn.net/u010929036/article/details/46345059

而且这道题会卡输入的,我用getline超时了。。。

#include<bits/stdc++.h>
#define de(x) cout<<#x<<"="<<x<<endl;
#define dd(x) cout<<#x<<"="<<x<<" ";
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define repd(i,a,b) for(int i=a;i>=(b);--i)
#define repp(i,a,b,t) for(int i=a;i<(b);i+=t)
#define ll long long
#define mt(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define pdd pair<double,double>
#define pdi pair<double,int>
#define mp(u,v) make_pair(u,v)
#define sz(a) (int)a.size()
#define ull unsigned long long
#define ll long long
#define pb push_back
#define PI acos(-1.0)
#define qc std::ios::sync_with_stdio(false)
#define db double
#define all(a) a.begin(),a.end()
const int mod = 1e9+7;
const int maxn = 1e3+6;
const double eps = 1e-6;
using namespace std;
bool eq(const db &a, const db &b) { return fabs(a - b) < eps; }
bool ls(const db &a, const db &b) { return a + eps < b; }
bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); }
ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); };
ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
ll kpow(ll a,ll b) {ll res=1; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;}
ll read(){
    ll x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
class Hamilton {
        int n, next[maxn];
        bool g[maxn][maxn], vis[maxn];

        int find(int u)
        {
                for (int v = 0; v < n; ++v)
                        if (g[u][v] && !vis[v]) return v;
                return -1;
        }

        void reverse(int v, int f)
        {
                if (v == -1) return;
                reverse(next[v], v); next[v] = f;
        }

        public:
        void init(int n)
        {
                this->n = n;
                memset(g, false, sizeof(g));
        }

        void add_edge(int u, int v) { g[u][v] = true; }

        void find_path(int s = 0)
        {
                int t = s, sz = 1;
                memset(next, -1, sizeof(next));
                memset(vis, false, sizeof(vis)); vis[s] = true;
                while (sz < n) {
                        if (sz == 1) {
                                for (int v; ~(v = find(s)); s = v)
                                        ++sz, vis[v] = true, next[v] = s;
                                for (int v; ~(v = find(t)); t = v)
                                        ++sz, vis[v] = true, next[t] = v;
                        } else {
                                for (int u, v = 0; v < n; ++v) if (!vis[v]) {
                                        ++sz, vis[v] = true;
                                        for (u = s; !g[u][v]; u = next[u]);
                                        s = next[u]; t = next[u] = v; break;
                                }
                        }
                        if (g[t][s]) next[t] = s;
                        for (int u = next[s], v; next[t] == -1; u = next[u])
                                if (g[u][t] && g[v=next[u]][s])
                                        reverse(v, s), next[u] = t, t = v;
                }
                for (int i = 0, u = 0; i < n; ++i, u = next[u])
                        printf("%d ", u + 1);
                printf("%d\n", 1);
        }
} grp;
char s[100011],*p;
//s可以对应char *,而不能对应char* & 
bool get_int(int &v,char* &p){
    v = 0;
    while(*p && !isdigit(*p)) p++;
    if(!isdigit(*p)) return false;
    while(isdigit(*p)) v = v * 10 + *p++ - '0';
    return true;
}
int main(){
    int n,v;
    scanf("%d",&n);getchar();
    grp.init(n);
    rep(i,0,n){
        gets(s);p = s;
//        de(s) 
        while(get_int(v,p)) grp.add_edge(i,--v);
    }
    grp.find_path();
    return 0;
}
View Code

 SGU  178

题意:题意自己了解下,打字不好描述

收获:打表模拟几组数据,暴力

#include<bits/stdc++.h>
#define de(x) cout<<#x<<"="<<x<<endl;
#define dd(x) cout<<#x<<"="<<x<<" ";
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define repd(i,a,b) for(int i=a;i>=(b);--i)
#define repp(i,a,b,t) for(int i=a;i<(b);i+=t)
#define ll long long
#define mt(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define pdd pair<double,double>
#define pdi pair<double,int>
#define mp(u,v) make_pair(u,v)
#define sz(a) (int)a.size()
#define ull unsigned long long
#define ll long long
#define pb push_back
#define PI acos(-1.0)
#define qc std::ios::sync_with_stdio(false)
#define db double
#define all(a) a.begin(),a.end()
const int mod = 1e9+7;
const int maxn = 2e2+5;
const double eps = 1e-6;
using namespace std;
bool eq(const db &a, const db &b) { return fabs(a - b) < eps; }
bool ls(const db &a, const db &b) { return a + eps < b; }
bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); }
ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); };
ll lcm(ll a,ll b) { return a/gcd(a,b)*b; }
ll kpow(ll a,ll b) {ll res=1; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;}
ll read(){
    ll x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
ll n;
bool ok(int i){
    if(!i) return n==1;
    ll sum = i,now = i + 1, cnt = 2 * i + 1;
    if(sum >= n) return true;
    rep(j, 0, cnt - i){
        sum += now;
        now <<= 1;
        if(sum >= n) return true;
    }
    return sum >= n;
}
int main(){
    scanf("%lld",&n);
    rep(i,0,101) if(ok(i)) return printf("%d\n",i),0;
    return 0;
}
View Code

 

posted on 2018-05-27 09:17  chinacwj1  阅读(148)  评论(0编辑  收藏  举报

导航