2021-09-23每日刷题打卡

一、POJ-1737:Connected Graph

1.1 问题描述

An undirected graph is a set V of vertices and a set of E∈{V*V} edges. An undirected graph is connected if and only if for every pair (u,v) of vertices,u is reachable from v.(当且仅当对于每一对(u,v)顶点,u可从v到达时,无向图才是连接的)
You are to write a program that tries to calculate the number of different connected undirected graph with n vertices.
For example, there are 4 different connected undirected graphs with 3 vertices.

img

题目大意:

给你一个有V个顶点和E(E<V*V)条边的无向图,请你计算出有多少种连通的无向图。

1.2 问题解决

#include<iostream>
#include<cstring>
#define rg register
#define il inline
#define co const

template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
    while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
    return data*w;
}

template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;

co int N=60,S=600;
int n;
struct A{
	int a[S],len;
	il A operator/(co int x)co{
		A ans;
		memset(ans.a,0,sizeof ans.a);
		ans.len=0;
		int num=0;
		for(int i=len;i;--i){
			num=num*10+a[i];
			ans.a[i]=num/x;
			num%=x;
			if(!ans.len&&ans.a[i]) ans.len=i;
		}
		return ans;
	}
	il A operator+(co A&x)co{
		A ans;
		memset(ans.a,0,sizeof ans.a);
		for(int i=1;i<=max(len,x.len);++i){
			ans.a[i]+=a[i]+x.a[i];
			ans.a[i+1]=ans.a[i]/10;
			ans.a[i]%=10;
		}
		ans.len=max(len,x.len);
		if(ans.a[ans.len+1]) ++ans.len;
		return ans;
	}
	il A operator*(co A&x)co{
		A ans;
		memset(ans.a,0,sizeof ans.a);
		for(int i=1;i<=len;++i)
			for(int j=1;j<=x.len;++j){
				ans.a[i+j-1]+=a[i]*x.a[j];
				ans.a[i+j]+=ans.a[i+j-1]/10;
				ans.a[i+j-1]%=10;
			}
		ans.len=len+x.len-1;
		if(ans.a[ans.len+1]) ++ans.len;
		return ans;
	}
}f[N],p[N];
il A C(int x,int y){
	A ans;
	ans.len=ans.a[1]=1;
	for(int i=y,j=1;j<=x;--i,++j){
		int t=i;
		A tmp;
		tmp.len=0;
		while(t){
			tmp.a[++tmp.len]=t%10;
			t/=10;
		}
		ans=ans*tmp/j;
	}
	return ans;
}
il void print(co A&x){
	for(int i=x.len;i;--i) printf("%d",x.a[i]);
	puts("");
}
int main(){
	for(int i=1;i<=50;++i){
		ll t=(1LL<<i)-1;
		while(t){
			p[i].a[++p[i].len]=t%10;
			t/=10;
		}
	}
	f[1].len=f[2].len=f[1].a[1]=f[2].a[1]=1;
	for(int i=3;i<=50;++i)
		for(int j=1;j<=i-1;++j)
			f[i]=f[i]+C(j-1,i-2)*f[j]*f[i-j]*p[j];
	while(read(n)) print(f[n]);
	return 0;
}

二、Leetcode-21:合并两个有序链表

2.1 问题描述

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

img

2.2 问题解决

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head = new ListNode();
        head.next = l1;
        ListNode last = head; 

        if(l1 == null) return l2;  

        while(l1 != null)
        {
            if(l2 == null)
                return head.next;
            
            if(l1.val <= l2.val)
            {
                last = l1;
                l1 = l1.next;
            }
            else
            {
                ListNode tmp = l2;
                l2 = l2.next;
                tmp.next = l1;
                last.next = tmp;
                last = tmp;
                l1 = tmp.next;
            }
        }

        if(l2 != null)
            last.next = l2;

        return head.next;
    }
}

三、生词

graph n. 图

undirected grap n. 无向图

set n. 集合

vertex n. 顶点

if and only if 当且仅当

四、参考文章

  1. 【POJ1737】Connected Graph
  2. POJ - 1737 Connected Graph【计数DP】
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值