码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #369 (Div. 2)

时间:2019-03-23 22:11:54      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:utc   $2   ted   str   暴力   set   directed   syn   ack   

C. Coloring Trees

O(n^4)暴力DP就好了

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl ‘\n‘
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head




const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, k;
ll dp[111][111][111];
int c[111][111], a[111];
void chkmin(ll &x, ll y) {x=min(x,y);} 

int main() {
	scanf("%d%d%d", &n, &m, &k);
	if (m==1&&k>1) return puts("-1");
	REP(i,1,n) scanf("%d", a+i);
	REP(i,1,n) REP(j,1,m) scanf("%d", &c[i][j]);
	memset(dp,0x3f,sizeof dp);
	dp[0][1][1] = 0;
	REP(i,1,n) REP(j,1,k) REP(pre,1,m) {
		int nxt;
		if (a[i]) { 
			if (i==1) nxt=1;
			else nxt=j+(pre!=a[i]);
			chkmin(dp[i][nxt][a[i]],dp[i-1][j][pre]);
			continue;
		}
		REP(now,1,m) { 
			if (i==1) nxt=1;
			else nxt=j+(pre!=now);
			chkmin(dp[i][nxt][now],dp[i-1][j][pre]+c[i][now]);
		}
	}
	ll ans = INF;
	REP(j,1,m) chkmin(ans,dp[n][k][j]);
	printf("%lld\n", ans==INF?-1:ans);
} 

D. Directed Roads

每个点出度为1, 那么图是一个基环树森林, 再观察一下可以发现每个连通块的答案为$2^n-2^{环的数量+非环边数}$

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl ‘\n‘
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head




const int N = 1e6+10;
int n, ans, sz, tot;
int dfn[N], fa[N];
vector<int> g[N];
void dfs(int x) {
    dfn[x] = ++*dfn;
	++tot;
    for (int y:g[x]) {
        if (dfn[y]) {
            if (dfn[y]<dfn[x]) continue;
            for (; y!=x; y=fa[y]) ++sz;
        }
        else fa[y]=x, dfs(y);
    }
}


int main() {
	scanf("%d", &n);
	REP(i,1,n) {
		int t;
		scanf("%d", &t);
		g[t].pb(i);
		g[i].pb(t);
	}
	int ans = 1;
	REP(i,1,n) if (!dfn[i]) { 
		tot=sz=0,dfs(i);
		ll t = qpow(2,tot)-qpow(2,tot-sz);
		ans = ans*t%P;
	}
	if (ans<0) ans+=P;
	printf("%d\n", ans);
}

 

Codeforces Round #369 (Div. 2)

标签:utc   $2   ted   str   暴力   set   directed   syn   ack   

原文地址:https://www.cnblogs.com/uid001/p/10585860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!