栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 3602 Count the Trees

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 3602 Count the Trees

#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <iostream>#include <string>#include <ctime>#include <algorithm>#include <cmath>#include <map>#include <ctime>using namespace std;typedef long long LL;typedef double DB;typedef unsigned UINT;typedef unsigned long long ULL;typedef vector<int> VI;typedef vector<char> VC;typedef vector<string> VS;typedef vector<LL> VL;typedef vector<DB> VF;const int dx4[] = {-1, 0, 1, 0};const int dy4[] = {0, 1, 0, -1};const int dx8[] = {-1, 0, 1, 0 , -1 , -1 , 1 , 1};const int dy8[] = {0, 1, 0, -1 , -1 , 1 , -1 , 1};const int MOD = 1000000007;const int INF = 100000;const LL INFF = 1LL << 60;const DB EPS = 1e-9;const DB OO = 1e15;const DB PI = acos(-1.0); //M_PI;#define SZ(A) int(A.size())#define PB push_back#define ALL(A) A.begin(), A.end()#define CPY(A, B) memcpy(A, B, sizeof(A))#define max(a,b) (a)>(b)?(a):(b)#define min(a,b) (a)<(b)?(a):(b)#define maxn 100010void PrintMsk(int MASK,int base,int nbit){for(int i=0;i<nbit;i++,MASK/=base)printf("%d ",MASK%base);printf("%c",10);}void Display(int* ARR,int LEFT,int RIGHT){for(int i=LEFT ; i<=RIGHT ; i++ )printf("%d ",ARR[i]);printf("%c",10);}struct matrix{int n; int a[25][25];void clear(){ memset(a,0,sizeof(a)); }void init(){ clear(); for(int i=1;i<=n;i++) a[i][i]=1; }matrix operator * (matrix b){matrix c; c.n=n;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++){c.a[i][j]=0;for(int k=1;k<=n;k++) c.a[i][j]+=a[i][k]*b.a[k][j];}return c;}matrix operator + (matrix b){matrix c; c.n=n;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)c.a[i][j]=a[i][j]+b.a[i][j];return c;}};LL ans;map<pair<int,int>,int>ha;int s1[maxn][2],s2[maxn][2],n,m,cnt[maxn],id,h1[maxn],h2[maxn];int gethash(int l,int r){if (ha[make_pair(l,r)])return ha[make_pair(l,r)];else{ha[make_pair(l,r)] = ++id;return id;}}int dfs1(int cur){int l,r;if (cur == -1) return 0;l = dfs1(s1[cur][0]);r = dfs1(s1[cur][1]);h1[cur] = gethash(l,r);cnt[h1[cur]]++;return h1[cur];}int dfs2(int cur){int l,r;if (cur == -1) return 0;l = dfs2(s2[cur][0]);r = dfs2(s2[cur][1]);h2[cur] = gethash(l,r);ans += cnt[h2[cur]];return h2[cur];}int main(){int i,cas;scanf("%d",&cas);while (cas--){scanf("%d%d",&n,&m);ha.clear();id = ans = 0;memset(cnt,0,sizeof(cnt));for ( i=1 ; i<=n ; i++ ) scanf("%d%d",&s1[i][0],&s1[i][1]);for ( i=1 ; i<=m ; i++ ) scanf("%d%d",&s2[i][0],&s2[i][1]);dfs1(1);dfs2(1);cout<<ans<<endl;}return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377228.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号