Counting Stars HDU - 6184(三元环计数)

Little A is an astronomy lover, and he has found that the sky was so beautiful!

So he is counting stars now!

There are n stars in the sky, and little A has connected them by m non-directional edges.

It is guranteed that no edges connect one star with itself, and every two edges connect different pairs of stars.

Now little A wants to know that how many different "A-Structure"s are there in the sky, can you help him?

An "A-structure" can be seen as a non-directional subgraph G, with a set of four nodes V and a set of five edges E.

If V=(A,B,C,D) and E=(AB,BC,CD,DA,AC), we call G as an "A-structure".

It is defined that "A-structure" G1=V1+E1 and G2=V2+E2 are same only in the condition that V1=V2 and E1=E2
.
Input There are no more than 300 test cases.

For each test case, there are 2 positive integers n and m in the first line.

2n105, 1mmin(2×105,n(n1)2)

And then m lines follow, in each line there are two positive integers u and v, describing that this edge connects node u and node v.

1u,vn

n3×105, m6×105
Output For each test case, just output one integer--the number of different "A-structure"s in one line.
Sample Input
4 5
1 2
2 3
3 4
4 1
1 3
4 6
1 2
2 3
3 4
4 1
1 3
2 4
Sample Output
1
6

题意:

给一个n个点m条边的无向图,要求统计满足star(四个点五条边的子图)的个数。(2<=n<=1e5, 1<=m<=min(1e5, n*(n-1)/2));

思路:

①统计每个点的度数
②入度<=sqrt(m)的分为第一类,入度>sqrt(m)的分为第二类
③对于第一类,暴力每个点,然后暴力这个点的任意两条边,再判断这两条边的另一个端点是否连接
因为m条边最多每条边遍历一次,然后暴力的点的入度<=sqrt(m),所以复杂度约为O(msqrt(m))
④对于第二类,直接暴力任意三个点,判断这三个点是否构成环,因为这一类点的个数不会超过sqrt(m)个,所以复杂度约为O(sqrt(m)3)=O(msqrt(m))
⑤判断两个点是否连接可以用set,map和Hash都行,根据具体情况而行
这种做法建的是双向边,常数很大

经分析1个star是由两个有一条相同边的不同的三元环构成的,所以问题就是对三元环计数。统计一个边能构成几个三元环,然后ans += C(cnt, 2)即可。

#include<cstdio>
#include<cstring>
#include<set>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int N = 1e5+5;
const int base = 1e5+5;
int deg[N],vis[N],bel[N];
vector<int>G[N];
set<LL>_hash;
int n,m;
void init(){
	for(int i=0;i<N;i++){
		vis[i]=deg[i]=bel[i]=0;
		G[i].clear();
	}
	_hash.clear();
}
void work(){
	int x=sqrt(m*1.0);
	LL ans=0,cnt;
	for(int a=1;a<=n;a++){
		vis[a]=1;
		for(int i=0;i<G[a].size();i++)
		   bel[G[a][i]]=a;   //确定与a连的所有点     
		for(int i=0;i<G[a].size();i++){   //寻找以ab为公共边的三角形 
			int b=G[a][i];
			cnt=0;
			if(vis[b]) continue;
			if(deg[b]<=x){  
				for(int j=0;j<G[b].size();j++){   //寻找与b连的所有点c,如果ac相连,则构成三角形   
					int c=G[b][j];
					if(bel[c]==a) cnt++;
				}
			}
			else{
				for(int j=0;j<G[a].size();j++){  //寻找与a连的另一点c 
					int c=G[a][j];
					if(_hash.find(1LL*base*b+c)!=_hash.end()) cnt++; //若bc相连,则构成三角形 
				}
			}
			ans+=(cnt-1)*cnt/2;
		}
	}
	printf("%lld\n",ans);
}
int main(){
	while(~scanf("%d%d",&n,&m)){
		init();
		for(int i=0;i<m;i++){
			int u,v;
			scanf("%d%d",&u,&v);
			deg[u]++;
			deg[v]++;
			G[u].push_back(v);
			G[v].push_back(u);
			_hash.insert(1LL*u*base+v);   
			_hash.insert(1LL*v*base+u);
		}
		work();
	}
	return 0;
}





### HDU 1466 Problem Description and Solution The problem **HDU 1466** involves calculating the expected number of steps to reach a certain state under specific conditions. The key elements include: #### Problem Statement Given an interactive scenario where operations are performed on numbers modulo \(998244353\), one must determine the expected number of steps required to achieve a particular outcome. For this type of problem, dynamic programming (DP) is often employed as it allows breaking down complex problems into simpler subproblems that can be solved iteratively or recursively with memoization techniques[^1]. In more detail, consider the constraints provided by similar problems such as those found in references like HDU 6327 which deals with random sequences using DP within given bounds \((1 \leq T \leq 10, 4 \leq n \leq 100)\)[^2]. These types of constraints suggest iterative approaches over small ranges might work efficiently here too. Additionally, when dealing with large inputs up to \(2 \times 10^7\) as seen in reference materials related to counting algorithms [^4], efficient data structures and optimization strategies become crucial for performance reasons. However, directly applying these methods requires understanding how they fit specifically into solving the expectation value calculation involved in HDU 1466. For instance, if each step has multiple outcomes weighted differently based on probabilities, then summing products of probability times cost across all possible states until convergence gives us our answer. To implement this approach effectively: ```python MOD = 998244353 def solve_expectation(n): dp = [0] * (n + 1) # Base case initialization depending upon problem specifics for i in range(1, n + 1): total_prob = 0 # Calculate transition probabilities from previous states for j in transitions_from(i): # Placeholder function representing valid moves prob = calculate_probability(j) next_state_cost = get_next_state_cost(j) dp[i] += prob * (next_state_cost + dp[j]) % MOD total_prob += prob dp[i] %= MOD # Normalize current state's expectation due to accumulated probability mass if total_prob != 0: dp[i] *= pow(total_prob, MOD - 2, MOD) dp[i] %= MOD return dp[n] # Example usage would depend heavily on exact rules governing transitions between states. ``` This code snippet outlines a generic framework tailored towards computing expectations via dynamic programming while adhering strictly to modular arithmetic requirements specified by the contest question format.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值